I am trying to set up unit testing in django using fixtures.
I can successfully load my fixtures, but when I attempt to retrieve data from them I get the error:
DoesNotExist: BlogIndexPage matching query does not exist.
Here is my code for the test (I'm using the Wagtail CMS, which extends unittest with a few additional methods):
class BlogTests(WagtailPageTests):
fixtures = ['demosite.json']
def test_can_create_blog_entry(self):
blog_index_page = BlogIndexPage.objects.get(pk=5)
self.assertCanCreate(blog_index_page, BlogPage, {
'title': 'Post 2',
'date': '2017-10-11',
'intro': 'Post 2',
'body': '<p>Test Post</p>'
})
And this is my fixture:
[
{
"pk": 1,
"model": "wagtailcore.page",
"fields": {
"title": "Root",
"draft_title": "Root",
"numchild": 1,
"show_in_menus": false,
"live": true,
"seo_title": "",
"depth": 1,
"search_description": "",
"content_type": [
"wagtailcore",
"page"
],
"has_unpublished_changes": false,
"owner": null,
"path": "0001",
"url_path": "/",
"slug": "root"
}
},
{
"pk": 2,
"model": "wagtailcore.page",
"fields": {
"title": "Home page",
"draft_title": "Home page",
"numchild": 5,
"show_in_menus": true,
"live": true,
"seo_title": "",
"depth": 2,
"search_description": "",
"content_type": [
"home",
"homepage"
],
"has_unpublished_changes": false,
"owner": null,
"path": "00010002",
"url_path": "/home-page/",
"slug": "home-page"
}
},
{
"pk": 5,
"model": "wagtailcore.page",
"fields": {
"title": "Blog index",
"draft_title": "Blog index",
"numchild": 3,
"show_in_menus": true,
"live": true,
"seo_title": "",
"depth": 3,
"search_description": "",
"content_type": [
"blog",
"blogindexpage"
],
"has_unpublished_changes": false,
"owner": null,
"path": "000100020002",
"url_path": "/blog/",
"slug": "blog"
}
},
{
"pk": 16,
"model": "wagtailcore.page",
"fields": {
"title": "Blog post",
"draft_title": "Blog post",
"numchild": 0,
"show_in_menus": false,
"live": true,
"seo_title": "",
"depth": 4,
"search_description": "The origin of the genus appears to be in the general area of Eastern Siberia/Mongolia. Wagtails spread rapidly across Eurasia and dispersed to Africa in the Zanclean (Early Pliocene) where the sub-Saharan lineage was later isolated. The African Pied Wagtail (and possibly the Mekong Wagtail) diverged prior to the massive radiation of the white-bellied black-throated and most yellow-bellied forms, all of which took place during the late Piacenzian (early Late Pliocene), c. 3 mya.",
"content_type": [
"blog",
"blogpage"
],
"has_unpublished_changes": false,
"owner": null,
"path": "0001000200020001",
"url_path": "/home-page/blog-index/blog-post/",
"slug": "blog-post"
}
}
]
So basically I just want to grab that blog index page, and test whether I can create a blogpage (blog post) underneath it. What am I doing wrong?
Your fixture needs to include records for "model": "blog.blogindexpage" as well as "model": "wagtailcore.page", with matching pk values. This is because Wagtail uses multi-table inheritance to represent pages: the data for a page is split across the wagtailcore_page table (which contains the core fields common to all page types, such as title) and another table such as blog_blogindexpage for each page model, containing the additional fields defined for that specific model. Without records in both tables, a lookup on BlogIndexPage will return no results, causing the DoesNotExist error above.
You can run ./manage.py dumpdata --indent 4 to get a dump of your development database in the JSON format used by fixtures; depending on the needs of your tests, you can either use this directly (./manage.py dumpdata --indent 4 > blog/fixtures/demosite.json) or use it as a guide for writing your own fixture manually.
Related
I have a document that looks like this:
{
"_id": "cc3a8d7f-5962-47e9-a3eb-09b0a57c9fdb",
"isDeleted": false,
"user": {
"timestamp": "2023-02-12",
"name": "john",
"surname": "doe",
"email": "a.s#ug.bilkent.edu.tr",
"phone": "+012345678912",
"age": 25,
"gender": "female",
"nationality": "smth",
"universityMajor": "ENGINEERING",
"preferences": null,
"highPrivacy": false,
},
"postings": [
{
"id": "f61b103d-8118-4054-8b24-b26e2f4febc4",
"isDeleted": false,
"timestamp": "2023-02-12",
"houseType": "apartment",
"totalNumOfRoommates": 5,
"location": {
"neighborhood": "Oran",
"district": "Çankaya",
"city": "Adana"
},
"startDate": "2022-11-10",
"endDate": "2022-11-15",
"postingType": "House Sharer",
"title": "House sharer post 1",
"description": "This is house sharer post 1",
"price": 2500,
"houseSize": "2 + 0"
},
{
"id": "b7d34113-1b13-4265-ba9b-766accecd267",
"isDeleted": false,
"timestamp": "2023-02-12",
"houseType": "apartment",
"totalNumOfRoommates": 5,
"location": {
"neighborhood": "Dikmen",
"district": "Çankaya",
"city": "Adana"
},
"startDate": "2022-09-13",
"endDate": "2023-12-24",
"postingType": "House Seeker",
"startPrice": 2002,
"endPrice": 2500
}
],
}
Each posting object has an ID. I am trying to "delete" (setting the property isDeleted to True, rather than actual deletion) the post whose ID is specified in the code below:
#router.delete('/{id}', response_description='Deletes a single posting')
async def deletePost(id: str):
update_result = await dbConnection.update_one({"postings.id": id, "postings.isDeleted" : False},
{"$set" : {"postings.$.isDeleted" : True} })
if update_result.modified_count == 1:
return Response(status_code=status.HTTP_204_NO_CONTENT)
else:
raise HTTPException(status_code=404, detail=f"Post {id} not found or has already been deleted")
The issue is that the first document (the one with ID f61b103d-8118-4054-8b24-b26e2f4febc4) is being "deleted" even when I supply the ID b7d34113-1b13-4265-ba9b-766accecd267 to the function. If I hit the endpoint again with the same ID, it "deletes" the array elements in order regardless of which ID I supply. Even though I am using the positional operator to set the specific element's property isDeleted to True.
What exactly could be the problem here?
Here is a link with the earlier setup in Mongo Playground: https://mongoplayground.net/p/03HSkwDUPUE
P.S although #ray's answer does work in MongoPlayground, I had to change a couple of things in the query with FastAPI, for anyone interested, the working query is below:
update_result = await dbConnection.update_one(
{"postings.id": id},
{
"$set": {
"postings.$[p].isDeleted": True
}
},
upsert=True,
array_filters=[
{
"p.id": id,
"p.isDeleted": False
}]
)
Your query now is kind of like searching documents through 2 criteria in a "or" behaviour.
"postings.id": "b7d34113-1b13-4265-ba9b-766accecd267" - find document with any postings element with id b7d34113-1b13-4265-ba9b-766accecd267
"postings.isDeleted": false - find document with any postings element with deleted is false
Note the "any". That means the 2 criteria are not required to be happened on the same array element. So, that is kind of "or" behaviour.
You can use arrayFilters to achieve what you want.
db.collection.update({},
{
"$set": {
"postings.$[p].isDeleted": true
}
},
{
arrayFilters: [
{
"p.id": "b7d34113-1b13-4265-ba9b-766accecd267",
"p.isDeleted": false
}
]
})
Mongo Playground
I need help with understanding how to parse json text for my assignment, and I can't use JSON library as it is not permitted, hence only 're'.
This is part of the file (wrapped for legibility, originally it's all on one line):
{
"unit": {
"title": "Web Development courses",
"raw_title": "{} courses",
"source_objects": [{
"type": "subcategory",
"id": 8,
"description": "Learn web development skills to build fully functioning websites.",
"url": "/courses/development/web-development/",
"title": "Web Development"
}],
"item_type": "course",
"items": [{
"_class": "course",
"id": 625204,
"title": "The Web Developer Bootcamp 2021",
"url": "/course/the-web-developer-bootcamp/",
"is_paid": true,
"visible_instructors": [{
"image_100x100": "https://img-c.udemycdn.com/user/100x100/4466306_6fd8_3.jpg",
"id": 4466306,
"url": "/user/coltsteele/",
"initials": "CS",
"display_name": "Colt Steele",
"image_50x50": "https://img-c.udemycdn.com/user/50x50/4466306_6fd8_3.jpg",
"job_title": "Developer and Bootcamp Instructor",
"name": "Colt",
"_class": "user",
"title": "Colt Steele"
}],
"image_125_H": "https://img-c.udemycdn.com/course/125_H/625204_436a_3.jpg",
"image_240x135": "https://img-c.udemycdn.com/course/240x135/625204_436a_3.jpg",
"is_practice_test_course": false,
"image_480x270": "https://img-c.udemycdn.com/course/480x270/625204_436a_3.jpg",
"published_title": "the-web-developer-bootcamp",
"tracking_id": "2zX-e8qKRUGgpwfF5AQx3w",
"headline": "COMPLETELY REDONE - The only course you need to learn web development - HTML, CSS, JS, Node, and More!",
"num_subscribers": 713744,
"avg_rating": 4.6973553,
"avg_rating_recent": 4.6947985,
"rating": 4.6947985,
"num_reviews": 215075,
"is_wishlisted": false,
"num_published_lectures": 610,
"num_published_practice_tests": 0,
"image_50x50": "https://img-c.udemycdn.com/course/50x50/625204_436a_3.jpg",
"image_100x100": "https://img-c.udemycdn.com/course/100x100/625204_436a_3.jpg",
"image_304x171": "https://img-c.udemycdn.com/course/304x171/625204_436a_3.jpg",
"image_750x422": "https://img-c.udemycdn.com/course/750x422/625204_436a_3.jpg",
"is_in_user_subscription": false,
"locale": {
"english_title": "English (US)",
"locale": "en_US",
"_class": "locale",
"simple_english_title": "English",
"title": "English (US)"
},
"has_closed_caption": true,
"caption_languages": ["English [Auto]", "French [Auto]", "German [Auto]", "Italian [Auto]", "Polish [Auto]", "Portuguese [Auto]", "Spanish [Auto]"],
"created": "2015-09-28T21:32:19Z",
"instructional_level": "All Levels",
"instructional_level_simple": "All Levels",
"content_length_practice_test_questions": 0,
"is_user_subscribed": false,
"buyable_object_type": "course",
"published_time": "2015-11-02T21:13:27Z",
"objectives_summary": ["The ins and outs of HTML5, CSS3, and Modern JavaScript for 2021", "Make REAL web applications using cutting-edge technologies", "Create responsive, accessible, and beautiful layouts"],
"is_recently_published": false,
"last_update_date": "2021-09-09",
"preview_url": "/course/625204/preview/",
"learn_url": "/course/the-web-developer-bootcamp/learn/",
"predictive_score": null,
"relevancy_score": null,
"input_features": null,
"lecture_search_result": null,
"curriculum_lectures": [],
"order_in_results": null,
"curriculum_items": [],
"instructor_name": null,
"content_info": "63.5 total hours",
"content_info_short": "63.5 hours",
"bestseller_badge_content": null,
"badges": [],
"free_course_subscribe_url": null,
"context_info": {
"subcategory": null,
"category": {
"tracking_object_type": "cat",
"id": 288,
"url": "/courses/development/",
"title": "Development"
},
"label": {
"id": 8322,
"display_name": "Web Development",
"topic_channel_url": "/topic/web-development/",
"url": "/topic/web-development/",
"title": "Web Development",
"tracking_object_type": "cl"
}
}
},
{
"_class": "course",
"title": "etc",
}]
}
}
I have about 200 more lines like these, each filled with course/student etc etc info and I need to extract info from certain keywords from this json file.
For example, for course information, I need to extract course_id, 100x100 img, course_title, subscribers etc, and similarly for students, and instructors I need to extract some more information.
My questions are:
since the three classes (course, students, instructors) share some keywords like ID, how do I pull courseID, studentID, instructorID separately?
for course ID,
I checked another link on stackoverflow and tried this but it didn't work so please help me out with this:
import re, os
raw_data_read = open('raw_data.txt', 'r')
regex_pattern = r'.*id\":\"([^\"]+)\".*'
match = re.match(regex_pattern, str(raw_data_read))
courseId= match.group(1)
print('courseId: {}'.format(courseId))
My Function app is not showing up in the Portal though it publishes successfully from local machine.
Example:
Success!:
Failure:
Tried:
Visiting https:myapp.azurewebsites.net
Visiting https://myapp.scm.azurewebsites.net/api/deployments
Last deployment looks good:
{
"id": "<redacted>",
"status": 4,
"status_text": "",
"author_email": "N/A",
"author": "<redacted>",
"deployer": "Push-Deployer",
"message": "Created via a push deployment",
"progress": "",
"received_time": "2020-10-23T20:38:47.1069702Z",
"start_time": "2020-10-23T20:38:47.3075338Z",
"end_time": "2020-10-23T20:39:19.3128742Z",
"last_success_end_time": "2020-10-23T20:39:19.3128742Z",
"complete": true,
"active": true,
"is_temp": false,
"is_readonly": true,
"url": "https://myapp.scm.azurewebsites.net/api/deployments/5e446e26fc934a75b4ab7241ce589e9f",
"log_url": "https://myapp.scm.azurewebsites.net/api/deployments/5e446e26fc934a75b4ab7241ce589e9f/log",
"site_name": "myapp"
}
Visiting https://myapp.scm.azurewebsites.net/api/deployments/5e446e26fc934a75b4ab7241ce589e9f/log
Steps looks good:
[
{
"log_time": "2020-10-23T20:38:47.2134775Z",
"id": "a9bfd9e3-dee2-4bfc-9e82-9b003d3df991",
"message": "Updating submodules.",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:38:47.2820867Z",
"id": "e980bad8-ee6a-467f-98ea-c78135e29eeb",
"message": "Preparing deployment for commit id '5e446e26fc'.",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:38:47.4606839Z",
"id": "961ce7eb-2388-4a36-9646-32a7455fce10",
"message": "Repository path is /tmp/zipdeploy/extracted",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:38:47.5141536Z",
"id": "1f1ad9b0-d5bf-41f0-ad77-537c2da9a010",
"message": "Running oryx build...",
"type": 1,
"details_url": "https://myapp.scm.azurewebsites.net/api/deployments/5e446e26fc934a75b4ab7241ce589e9f/log/1f1ad9b0-d5bf-41f0-ad77-537c2da9a010"
},
{
"log_time": "2020-10-23T20:39:14.3176646Z",
"id": "45fc756f-4873-469c-8adf-7b8968ceec95",
"message": "Writing the artifacts to a Zip file",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:39:18.8047683Z",
"id": "3f9b3434-dcbc-496e-8480-d3bff123159e",
"message": "Running post deployment command(s)...",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:39:18.9330242Z",
"id": "11e6d453-3aa0-4afd-a20e-7edc6369d774",
"message": "Triggering recycle (preview mode disabled).",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:39:19.0915157Z",
"id": "43df8884-3066-434b-91ed-6b2bbfac6236",
"message": "Deployment successful.",
"type": 0,
"details_url": null
}
]
I even created a whole new Function App (in the same resource group, using the same App Service Plan) and tried publishing the Function to it... no go.
I have 6 or 7 python Functions in prod, this is not my first deployment.
Is this a known issue that is being worked on? How do I get the Function to be visible in Portal?
I was able to determine the issue. It was my fault.
SSH into Function app via https://myFunctionName.scm.azurewebsites.net/webssh/host
ls /home/site/wwwroot/myFunctionName
The main python filename had a space " " in the filename
Was: __init__ .py
Should have been: __init__.py
Fixed this and republished
I am currently making a scraper app, but before going full out with the app, using other frameworks like Discord.py, I had to first scrape the site first. It proved quite difficult to scrape the site. The site that I am trying to scrape from is Fiverr. Anyways, long story short, I had to get some cookies to login with Python Requests. The big issue now is that the data I need to scrape comes in the form of JSON, which I don't know much about. I managed to select the javascript in question, but once I load it it gives an error: "TypeError: the JSON object must be str, bytes or bytearray, not Tag". I specifically need the "rows" part which is part of the JSON data.
I'm not quite certain how to fix this and have read and tried some similar questions here. I will appreciate any help.
import requests
from bs4 import BeautifulSoup
import re
import json
# Irrelevant to the question
class JobClass:
def __init__(self, date=None, buyer=None, request=None, duration=None, budget=None, link="https://www.fiverr.com/users/myusername/requests", id=None):
self.date = date
self.buyer = buyer
self.request = request
self.duration = duration
self.budget = budget
self.link = link
self.id = id
# Irrelevant to the question
duplicateSet = set()
scrapedSet = set()
jobObjArr = []
headers = {
# Some private cookies. To get them you just need to use a site like https://curl.trillworks.com/ it is really a life saver
# This is used to tell the site who you are to be logged in (which is why I deleted this part out of the code)
}
# Please note that I used "myusername" in the URL. This is going to be different depending on user
# Using the requests module, we use the "get" function
# provided to access the webpage provided as an
# argument to this function:
result = requests.get(
'https://www.fiverr.com/users/myusername/requests', headers=headers)
# Now, let us store the page content of the website accessed
# from requests to a variable:
src = result.content
# Now that we have the page source stored, we will use the
# BeautifulSoup module to parse and process the source.
# To do so, we create a BeautifulSoup object based on the
# source variable we created above:
soup = BeautifulSoup(src, "lxml")
data = soup.select("[type='text/javascript']")[1]
print(data)
# TypeError: the JSON object must be str, bytes or bytearray, not Tag
jsonObject = json.loads(data)
# Here is the output of print(data):
<script type="text/javascript">
document.viewData = {
"dds": {
"subCats": {
"current": {
"text": "All Subcategories",
"val": "-1"
},
"options": [{
"text": "Web \u0026 Mobile Design",
"val": 151
}, {
"text": "Web Programming",
"val": 140
}]
}
},
"results": {
"rows": [{
"type": "none",
"identifier": "5cf132b55e08360011efe633",
"cells": [{
"text": "May 31, 2019",
"type": "date",
"withText": true
}, {
"userPict": "\u003cspan class=\"missing-image-user \"\u003ec\u003c/span\u003e",
"type": "profile-40",
"cssClass": "height95"
}, {
"hintBottom": false,
"text": "My website was hacked and deleted. Need to have it recreated ",
"type": "text-wide",
"tags": [],
"attachment": false
}, {
"text": 1,
"type": "applications",
"alignCenter": true
}, {
"text": "3 days",
"type": "hidden-action",
"actionVisible": false,
"alignCenter": true,
"withText": true,
"buttons": [{
"type": "span",
"text": "3 days",
"class": "duration"
}, {
"type": "button",
"text": "Remove Request",
"class": "remove-request js-remove-request",
"meta": {
"requestId": "5cf132b55e08360011efe633",
"isProfessional": false
}
}]
}, {
"text": "---",
"type": "hidden-action",
"actionVisible": false,
"alignCenter": true,
"withText": true,
"buttons": [{
"type": "span",
"text": "---",
"class": "budget"
}, {
"type": "button",
"text": "Send Offer",
"class": "btn-standard btn-green-grad js-send-offer",
"meta": {
"username": "conto217",
"category": 3,
"subCategory": 151,
"requestId": "5cf132b55e08360011efe633",
"requestText": "My website was hacked and deleted. Need to have it recreated ",
"userPict": "\u003cspan class=\"missing-image-user \"\u003ec\u003c/span\u003e",
"isProfessional": false,
"buyerId": 32969684
}
}]
}]
}, {
"type": "none",
"identifier": "5cf12f641b6e99000edf1b60",
"cells": [{
"text": "May 31, 2019",
"type": "date",
"withText": true
}, {
"userPict": "\u003cimg src=\"https://fiverr-res.cloudinary.com/t_profile_small,q_auto,f_auto/attachments/profile/photo/648ceb417a85844b25e8bf070a70d9a0-254781561534997516.9743/MyFileName\" alt=\"muazamkhokher\" width=\"40\" height=\"40\"\u003e",
"type": "profile-40",
"cssClass": "height95"
}, {
"hintBottom": false,
"text": "Need mobile ui/ux designer from marvel wireframes",
"type": "text-wide",
"tags": [],
"attachment": false
}, {
"text": 4,
"type": "applications",
"alignCenter": true
}, {
"text": "5 days",
"type": "hidden-action",
"actionVisible": false,
"alignCenter": true,
"withText": true,
"buttons": [{
"type": "span",
"text": "5 days",
"class": "duration"
}, {
"type": "button",
"text": "Remove Request",
"class": "remove-request js-remove-request",
"meta": {
"requestId": "5cf12f641b6e99000edf1b60",
"isProfessional": false
}
}]
}, {
"text": "$50",
"type": "hidden-action",
"actionVisible": false,
"alignCenter": true,
"withText": true,
"buttons": [{
"type": "span",
"text": "$50",
"class": "budget"
}, {
"type": "button",
"text": "Send Offer",
"class": "btn-standard btn-green-grad js-send-offer",
"meta": {
"username": "muazamkhokher",
"category": 3,
"subCategory": 151,
"requestId": "5cf12f641b6e99000edf1b60",
"requestText": "Need mobile ui/ux designer from marvel wireframes",
"userPict": "\u003cimg src=\"https://fiverr-res.cloudinary.com/t_profile_small,q_auto,f_auto/attachments/profile/photo/648ceb417a85844b25e8bf070a70d9a0-254781561534997516.9743/MyFileName\" alt=\"muazamkhokher\" width=\"100\" height=\"100\"\u003e",
"isProfessional": false,
"buyerId": 25478156
}
}]
}]
....
I expect the JSON to be loaded in jsonObject, but I get an error: "TypeError: the JSON object must be str, bytes or bytearray, not Tag"
Edit: Here is some code at the end of the print statement. It randomly cuts off for some reason with no ending script tag:
}, {
"type": "none",
"identifier": "5cf1236a959aa5000f1ce094",
"cells": [{
"text": "May 31, 2019",
"type": "date",
"withText": true
}, {
"userPict": "\u003cimg src=\"https://fiverr-res.cloudinary.com/t_profile_small,q_auto,f_auto/profile/photos/30069758/original/Universalco_2a_Cloud.png\" alt=\"clarky2000\" width=\"40\" height=\"40\"\u003e",
"type": "profile-40",
"cssClass": "height95"
}, {
"hintBottom": false,
"text": "Slider revolution slider. 3 slides for a music festival. I can supply a copy what each slide should look like (see attached) and all the individual objects. Anyone can create basic RS slides, but I want this to be dynamic as its for a music festival. We are using the free version of RS if were are required to use the paid version of SL for addons please let us know. Bottom line this must be 3 dynamic slides (using the same background) for a music festival audience. Unlimited revisions is a must.",
"type": "see-more",
"tags": [{
"text": "Graphic UI"
}, {
"text": "Landing Pages"
}],
"attachment": {
"url": "/download/file/1559260800%2Fgig_requests%2Fattachment_f2a5f51b9fb473e8fc7f498929f39e3f",
"name": "Outwith Rotator_1920x1080_1.jpg",
"size": "2.68 MB"
}
}, {
"text": 2,
"type": "applications",
"alignCenter": true
}, {
"text": "24 hours",
"type": "hidden-action",
"actionVisible": false,
"alignCenter": true,
"withText": true,
"buttons": [{
"type": "span",
"text": "24 hours",
"class": "duration"
}, {
"type": "button",
"text": "Remove Request",
"class": "remove-request js-remove-request",
"meta": {
"requestId": "5cf1236a959aa5000f1ce094",
"isProfessional": false
}
}]
}, {
"text": "$23",
"type": "hidden-action",
"actionVisible": false,
"alignCenter": true,
"withText": true,
"buttons": [{
"type": "span",
"text": "$23",
"class": "budget"
}, {
"type": "button",
"text": "Send Of
I'm running python manage.py loaddata 'path/to/mydata.json' with an empty database (User and UserProfile tables are created but not populated), however, I'm getting the following error:
django.db.utils.IntegrityError: Problem installing fixture 'path/to/mydata.json': Could not load myapp.UserProfile(pk=1): UNIQUE constraint failed: myapp_userprofile.user_id
I checked (even after running this command) and the database is not populated at all. So how can it be giving an error that the pk is not unique?
If relevant, UserProfile just extends the default User model with a OneToOneField relation, as proposed here.
Here is what mydata.json contains:
[
{
"model": "auth.user",
"pk": 1,
"fields": {
"password": "pbkdf2_sha256..",
"last_login": "2016-10-22T15:19:46.926Z",
"is_superuser": true,
"username": "thesuperuser",
"first_name": "",
"last_name": "",
"email": "a#a.co",
"is_staff": true,
"is_active": true,
"date_joined": "2016-10-22T14:48:27.394Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.user",
"pk": 2,
"fields": {
"password": "pbkdf2_sha256..",
"last_login": null,
"is_superuser": false,
"username": "user1",
"first_name": "User",
"last_name": "One",
"email": "",
"is_staff": false,
"is_active": true,
"date_joined": "2016-10-22T15:20:32Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.user",
"pk": 4,
"fields": {
"password": "pbkdf2_sha256..",
"last_login": null,
"is_superuser": false,
"username": "user3",
"first_name": "User",
"last_name": "Three",
"email": "",
"is_staff": false,
"is_active": true,
"date_joined": "2016-10-22T15:21:09Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.user",
"pk": 3,
"fields": {
"password": "pbkdf2_sha256..",
"last_login": null,
"is_superuser": false,
"username": "user2",
"first_name": "User",
"last_name": "Two",
"email": "",
"is_staff": false,
"is_active": true,
"date_joined": "2016-10-22T15:21:03Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "myapp.userprofile",
"pk": 1,
"fields": {
"user": 1,
"money": 100
}
},
{
"model": "myapp.userprofile",
"pk": 2,
"fields": {
"user": 2,
"money": 100
}
},
{
"model": "myapp.userprofile",
"pk": 3,
"fields": {
"user": 3,
"money": 100
}
},
{
"model": "myapp.userprofile",
"pk": 4,
"fields": {
"user": 4,
"money": 100
}
}
]
Thanks for any help,
Exclude ContentType and Auth Permissions objects when creating a db dump.
python manage.py dumpdata --natural-foreign --natural-primary -e contenttypes -e auth.Permission --indent 2 > dump.json
After that you should be able to run the command without any issue
python manage.py loaddata dump.json
Credit to
https://www.coderedcorp.com/blog/how-to-dump-your-django-database-and-load-it-into-/
for saving my day
Today (24th, April, 2020) I had a similar problem with Django 2.2
My fixtures file was as simple as this:
[
{
"model": "contenttypes.contenttype",
"pk": 1,
"fields": {
"app_label": "admin",
"model": "logentry"
}
},
{
"model": "contenttypes.contenttype",
"pk": 2,
"fields": {
"app_label": "auth",
"model": "permission"
}
}]
When I ran ./manage.py loaddata initial_data.json, I got:
django.db.utils.IntegrityError: Problem installing fixture '/home/user/reponame/projectname/initial_data.json': Could not load contenttypes.ContentType(pk=2): UNIQUE constraint failed: django_content_type.app_label, django_content_type.model
What I did to make it work was just rename the pk to id for the contenttypes.contenttype model. After that, the migration worked as expected.
./manage.py loaddata initial_data.json
Installed 2 object(s) from 1 fixture(s)
After the change, my initial_data.json file was:
[
{
"model": "contenttypes.contenttype",
"id": 1,
"fields": {
"app_label": "admin",
"model": "logentry"
}
},
{
"model": "contenttypes.contenttype",
"id": 2,
"fields": {
"app_label": "auth",
"model": "permission"
}
}]
It is worth mentioning that my original initial_dataj.json has many other models, but renaming pk to id only for the contenttypes.contenttype solved my problem.
I had a similar problem. Inspired by this post:
https://github.com/yourlabs/django-cities-light/issues/89 (See 'how to fix it')
before running the loaddata command, I commented the receiver signal decorator before the 'saving function', and it worked.
go the the json file and change evey single 'pk' to 'id'
if you use vs code you can just select 1 and press cmd/ctrl + f2 as a shortcut
Had the same problem while exporting and importing a model with a ManyToMany relation. This was, because I manually speficied the ManyToMany through model while exporting, what caused an unique constraint error.
class MyModel(model.Model):
groups = models.ManyToManyField(
'myapp.mymodel',
)
You only need to execute dumpdata myapp.mymodel, not `dumpdata myapp.mymodel myapp.mymodel_groups"
Otherwise, your through model data is in the export twice, and causes a unique constraint error.
It`s a good question how this behaves, when you specify an explicit through model...i don't know and have no time to test :)
Comment your signals while loading the fixture.