Can I get error index with bulk_create/bulk_update? - django

I'm using bulk_create method to insert data from an Excel sheet to db. This data can be incorrect (ex: cell contains "Hello World" when it should be an integer) and I'd like to return user thorough information error.
Currently, bulk_create raises a ValueError, ex: Field 'some_integer_field' expected a number but got 'Hello World'.
Is it possible to get more details about this error, something like : **Line 4** - Field 'some_integer_field' expected a number but got 'Hello World'. without saving each object individually ? Does it even make sense since bulk_create produces a single transaction ?
I'm running Django 4.1.3 with PostgreSQL 12.
My code looks like self.MyModel.bulk_create(self.objects_to_create)

Related

Django queryset - differing results for query and query.count()

I have a django model with three fields and I'm trying to find the duplicates. If I run:
cls.objects.values('institution','person','title').annotate(records=Count('person')).filter(records__gt=1).count() I get 152 as the output.
However, if I attempt to see what those records are and run the same query without the count() cls.objects.values('institution','person','title').annotate(records=Count('person')).filter(records__gt=1)
I get <QuerySet []>.
Any idea what's going on? If I add a .first() I get null, and a [0] gives me an out of range error, however the count continues to return 152. Running SQL directly on the database shows there are in fact 152 matching entries so the count is correct, but I can't seem to get the queryset (or any elements in it) to return.
The startdate injection into the GROUP BY mentioned by #JoVi in the comment turned out to be the issue here. It was being added due to it being a default sort in the django model definition. Adding an empty .order_by() to the query removed that and resulted in the query working as expected.

Django doesn't creates objects for all rows in postgresql

I'm scripting a migration tool to get my data from an old software to a new version with different data structures.
While looping through the csv-files I'm creating the objects.
The problem is that every object gets created in postgres but django doesn't.
I'm getting errors like:
duplicate key value violates unique constraint "api_order_pkey"
DETAIL: Key (id)=(159865) already exists.
Order.objects.get(pk=159865) does return "Does not exist"
But a SELECT * FROM api_order WHERE pk = 159865 directly in Postgres finds a row.
with open("order.csv","r") as f:
reader = csv.reader(f)
for row in reader:
Order.objects.create(pk = row[0], ordernr= row[1],....)`
thats of course a short version of it.
On the import of about 250.000 rows this problem appears with about 100 rows.
I had the same problem with deleting. I ran Order.objects.all().delete() but in Postgres there where still a few rows left while Order.objects.all() returned an empty Queryset.
Is there something I can do about this?
Has anyone had a similar problem?
Sorry, my fault.
I had a Manager running which was modifying the queryset.
Thats why Django did not show all records.

Database query failing in django views when using filter

Just going through the Django tutorial and am playing around and testing stuff.
My question is, how come the following line works and lets me go to page no problem:
test = Choice.choices.all()
While the following filter line gives me the error message ValueError: too many values to unpack (expected 2)
test = Choice.choices.get("question_id=6")
Even when I try the following two lines it doesn't work. No idea what's happening or why
test = Choice.choices.get("question_id=6")[0]
test = Choice.choices.get("question_id=6")[0].question_text
I feel like i need to really understand what's going on and why so I can actually do proper queries in the future
That's because of a syntax error. It should be:
test = Choice.choices.get(question_id="6")
or, if the question_id field is int (pretty sure it is):
test = Choice.choices.get(question_id=6)
Basically, what's inside the bracket (parameter) is the Django translation of a SQL WHERE condition, where you can specify what field and what value to include in the result.
Infact, it corresponds to: SELECT * FROM table WHERE question_id=6
You put this where condition inside a string, but the .get() method doesn't accept a string.
Let me know whether it works!

Django Model.save() works in a shell but not in a view?

For some reason when I call .save() on a model from within a view, the object isn't saved as a row in the database when called from a view. The id_seq table is incremented and has an id that doesn't exist in the table. The same code from the shell works as it should.
Example:
In a shell this will work:
app_version = AppVersion()
app_version.name = "some new name"
app_version.code = 880
app_version.save()
But in a View the same code does and the following happens
print str(app_version.id) // will return 99 for example
but:
select * from app_appversion order by id desc limit 1;
returns 98
the app_appversion_id_seq has 99 as the id.
Wtf is going on? This is the same problem as described here https://groups.google.com/forum/#!topic/django-users/lBMSM4R3GqI
Django version is 1.5.1. No errors are caught or thrown. Didn't have this problem back on 1.4.8...

Output location of images for django-imagekit

I'm trying to output a list of images that belong to each record in my app as below:
pri_photo = vehicle.images.all()[:1]
sec_photos = vehicle.images.all()[1:]
This first part is OK. The part I'm having issues with is when I try
pri_photo.original_image.url
sec_photos.original_image.url
The above two lines of code give me a 'QuerySet' object has no attribute 'original_image'. What could be the issue?
I also want the photos in sec_photos to be output as image1, image2,... upto the last one
[:1] just limits the number of records returned by the queryset, still it is a queryset result, not a single object.
to get a single object, you shoud use
[0]
or
[0:1].get()
but it's not safe, as it will raise an error if none objects match the query. to do it properly, use filter() and if result are present then get(), or
try:
#get()
except modelname.DoesNotExist:
# do shomething else
also, if you are looking for the latest object, maybe you can just use http://www.djangoproject.com/documentation/models/get_latest/