Consider the following models with the following fields:
Powers:
class Power(models.Model):
...
half = models.BooleanField()
micro = models.BooleanField()
UseP:
class UseP(models.Model):
...
power_policy = models.ForeignKey(Power, on_delete=models.CASCADE)
def use_types():
TYPES = []
TYPES.append(("F", "Full Use"))
if power_policy.half:
TYPES.append(("H", "Half Use"))
if power_policy.micro:
TYPES.append(("M", "Micro Use"))
return tuple(TYPES)
use_type = models.CharField(max_length=1, choices=use_types())
The function doesn't run. As you can see, when I try without the "self" arguments, it says that power_policy is not defined. If I do it like self.power_policy, it recognizes the power policy but then when I got and call the function like use_type = models.CharField(max_length=1, choices=self.use_types()), it says that the self keyword is not defined in this line.
I think the code explains it all, but in case it doesn't I want to provide choices to my user after they choose the Power, according to the power option. For which, I created a function but it doesn't really seem to work. What am I doing wrong or is it not even possible like this?
Thank You for your time,
If you create a method in your class, which uses fields from that class - you have to pass self argument to your method.
Change your method to: def use_types(self): then you can use your fields like self.power_policy.
You cannot use your field use_type like this. If you want to set use_type based on power_policy field you can do this in your save method like so:
def save(*args, **kwargs):
if self.power_policy and self.power_policy.half:
self.use_type = 'H'
elif self.power_policy and self.power_policy.micro:
self.use_type = 'M'
return super(UseP, self).save(*args, **kwargs)
Well, I have two django models for storing Questions and Questionnaires. Every question is inside a questionnaire. Every Question should have a field to store the order it is going to appear in a questionnaire. My questions is what is about the best way to handle changes in the order of a question.
The code have for the models:
class Questionnaire(models.Model):
name = models.CharField(max_length = 100)
time_creation = models.DateTimeField(auto_now=True)
class Question(models.Model):
questionnaire = models.ForeignKey(Questionnaire, related_name='questions', on_delete=models.CASCADE)
text = models.TextField()
order = models.IntegerField()
One way I thought of doing it was by overriding the save method for the Question model, like that:
def save(self, *args, **kwargs):
all_questions = self.questionnaire.questions.all()
if (self.order == None or self.order > len(all_questions)+1 or self.order == 0):
self.order = len(all_questions)+1 # starts in 1
else:
previous = all_questions.filter(order=self.order)
if len(previous)>0:
p = previous[0]
p.order = self.order+1
p.save()
super().save(*args, **kwargs)
It is kind of a recursive function, inside save() i verify if there is a Question with the same order number and increase it then call the save (inside the save), which will do the same for this object.
Is it a good way to solve the problem? Django has a few idiosyncrasies that i might not be aware of and might bite me latter. Should i use signals, maybe? Do it post save? Thank you for your help!
Django provides a very simple mechanism through model meta options, namely order_with_respect_to.
I believe using it would already make things easier:
class Question(models.Model):
questionnaire = models.ForeignKey(Questionnaire)
# ...
class Meta:
order_with_respect_to = 'questionnaire'
This adds two methods to the Questionnaire model. Both let you handle the order of the Question objects using a list of Question primary keys.
Get the order
questionnaire = Questionnaire.objects.get(id=1)
questionnaire.get_question_order()
[1, 2, 3]
Set the order
questionnaire.set_question_order([3, 1, 2])
Please note that this operation will add an _order column to the database and implicitly set ordering to use this.
You can now implement all further business logic by manipulating the order list. Just an example helper for finding the order of a Question object:
questionnaire = Questionnaire.objects.get(id=1)
question_order = questionnaire.get_question_order()
question = Question.objects.get(foo='bar')
position = question_order.index(question.pk)
I am not sure if you should really go down that road (using an order field), but if you do, at least use a PositiveIntegerField and constrain it to be unique. I don't know where your inputs comes from but you should always try to sanitise them to a sequence, throw them in a list, and then set the order of the Question objects all at once.
I am running into a database issue in my unit tests. I think it has something to do with the way I am using TestCase and setUpData.
When I try to set up my test data with certain values, the tests throw the following error:
django.db.utils.IntegrityError: duplicate key value violates unique constraint
...
psycopg2.IntegrityError: duplicate key value violates unique constraint "InventoryLogs_productgroup_product_name_48ec6f8d_uniq"
DETAIL: Key (product_name)=(Almonds) already exists.
I changed all of my primary keys and it seems to be running fine. It doesn't seem to affect any of the tests.
However, I'm concerned that I am doing something wrong. When it first happened, I reversed about an hour's worth of work on my app (not that much code for a noob), which corrected the problem.
Then when I wrote the changes back in, the same issue presented itself again. TestCase is pasted below. The issue seems to occur after I add the sortrecord items, but corresponds with the items above it.
I don't want to keep going through and changing primary keys and urls in my tests, so if anyone sees something wrong with the way I am using this, please help me out. Thanks!
TestCase
class DetailsPageTest(TestCase):
#classmethod
def setUpTestData(cls):
cls.product1 = ProductGroup.objects.create(
product_name="Almonds"
)
cls.variety1 = Variety.objects.create(
product_group = cls.product1,
variety_name = "non pareil",
husked = False,
finished = False,
)
cls.supplier1 = Supplier.objects.create(
company_name = "Acme",
company_location = "Acme Acres",
contact_info = "Call me!"
)
cls.shipment1 = Purchase.objects.create(
tag=9,
shipment_id=9999,
supplier_id = cls.supplier1,
purchase_date='2015-01-09',
purchase_price=9.99,
product_name=cls.variety1,
pieces=99,
kgs=999,
crackout_estimate=99.9
)
cls.shipment2 = Purchase.objects.create(
tag=8,
shipment_id=8888,
supplier_id=cls.supplier1,
purchase_date='2015-01-08',
purchase_price=8.88,
product_name=cls.variety1,
pieces=88,
kgs=888,
crackout_estimate=88.8
)
cls.shipment3 = Purchase.objects.create(
tag=7,
shipment_id=7777,
supplier_id=cls.supplier1,
purchase_date='2014-01-07',
purchase_price=7.77,
product_name=cls.variety1,
pieces=77,
kgs=777,
crackout_estimate=77.7
)
cls.sortrecord1 = SortingRecords.objects.create(
tag=cls.shipment1,
date="2015-02-05",
bags_sorted=20,
turnout=199,
)
cls.sortrecord2 = SortingRecords.objects.create(
tag=cls.shipment1,
date="2015-02-07",
bags_sorted=40,
turnout=399,
)
cls.sortrecord3 = SortingRecords.objects.create(
tag=cls.shipment1,
date='2015-02-09',
bags_sorted=30,
turnout=299,
)
Models
from datetime import datetime
from django.db import models
from django.db.models import Q
class ProductGroup(models.Model):
product_name = models.CharField(max_length=140, primary_key=True)
def __str__(self):
return self.product_name
class Meta:
verbose_name = "Product"
class Supplier(models.Model):
company_name = models.CharField(max_length=45)
company_location = models.CharField(max_length=45)
contact_info = models.CharField(max_length=256)
class Meta:
ordering = ["company_name"]
def __str__(self):
return self.company_name
class Variety(models.Model):
product_group = models.ForeignKey(ProductGroup)
variety_name = models.CharField(max_length=140)
husked = models.BooleanField()
finished = models.BooleanField()
description = models.CharField(max_length=500, blank=True)
class Meta:
ordering = ["product_group_id"]
verbose_name_plural = "Varieties"
def __str__(self):
return self.variety_name
class PurchaseYears(models.Manager):
def purchase_years_list(self):
unique_years = Purchase.objects.dates('purchase_date', 'year')
results_list = []
for p in unique_years:
results_list.append(p.year)
return results_list
class Purchase(models.Model):
tag = models.IntegerField(primary_key=True)
product_name = models.ForeignKey(Variety, related_name='purchases')
shipment_id = models.CharField(max_length=24)
supplier_id = models.ForeignKey(Supplier)
purchase_date = models.DateField()
estimated_delivery = models.DateField(null=True, blank=True)
purchase_price = models.DecimalField(max_digits=6, decimal_places=3)
pieces = models.IntegerField()
kgs = models.IntegerField()
crackout_estimate = models.DecimalField(max_digits=6,decimal_places=3, null=True)
crackout_actual = models.DecimalField(max_digits=6,decimal_places=3, null=True)
objects = models.Manager()
purchase_years = PurchaseYears()
# Keep manager as "objects" in case admin, etc. needs it. Filter can be called like so:
# Purchase.objects.purchase_years_list()
# Managers in docs: https://docs.djangoproject.com/en/1.8/intro/tutorial01/
class Meta:
ordering = ["purchase_date"]
def __str__(self):
return self.shipment_id
def _weight_conversion(self):
return round(self.kgs * 2.20462)
lbs = property(_weight_conversion)
class SortingModelsBagsCalulator(models.Manager):
def total_sorted(self, record_date, current_set):
sorted = [SortingRecords['bags_sorted'] for SortingRecords in current_set if
SortingRecords['date'] <= record_date]
return sum(sorted)
class SortingRecords(models.Model):
tag = models.ForeignKey(Purchase, related_name='sorting_record')
date = models.DateField()
bags_sorted = models.IntegerField()
turnout = models.IntegerField()
objects = models.Manager()
def __str__(self):
return "%s [%s]" % (self.date, self.tag.tag)
class Meta:
ordering = ["date"]
verbose_name_plural = "Sorting Records"
def _calculate_kgs_sorted(self):
kg_per_bag = self.tag.kgs / self.tag.pieces
kgs_sorted = kg_per_bag * self.bags_sorted
return (round(kgs_sorted, 2))
kgs_sorted = property(_calculate_kgs_sorted)
def _byproduct(self):
waste = self.kgs_sorted - self.turnout
return (round(waste, 2))
byproduct = property(_byproduct)
def _bags_remaining(self):
current_set = SortingRecords.objects.values().filter(~Q(id=self.id), tag=self.tag)
sorted = [SortingRecords['bags_sorted'] for SortingRecords in current_set if
SortingRecords['date'] <= self.date]
remaining = self.tag.pieces - sum(sorted) - self.bags_sorted
return remaining
bags_remaining = property(_bags_remaining)
EDIT
It also fails with integers, like so.
django.db.utils.IntegrityError: duplicate key value violates unique constraint "InventoryLogs_purchase_pkey"
DETAIL: Key (tag)=(9) already exists.
UDPATE
So I should have mentioned this earlier, but I completely forgot. I have two unit test files that use the same data. Just for kicks, I matched a primary key in both instances of setUpTestData() to a different value and sure enough, I got the same error.
These two setups were working fine side-by-side before I added more data to one of them. Now, it appears that they need different values. I guess you can only get away with using repeat data for so long.
I continued to get this error without having any duplicate data but I was able to resolve the issue by initializing the object and calling the save() method rather than creating the object via Model.objects.create()
In other words, I did this:
#classmethod
def setUpTestData(cls):
cls.person = Person(first_name="Jane", last_name="Doe")
cls.person.save()
Instead of this:
#classmethod
def setUpTestData(cls):
cls.person = Person.objects.create(first_name="Jane", last_name="Doe")
I've been running into this issue sporadically for months now. I believe I just figured out the root cause and a couple solutions.
Summary
For whatever reason, it seems like the Django test case base classes aren't removing the database records created by let's just call it TestCase1 before running TestCase2. Which, in TestCase2 when it tries to create records in the database using the same IDs as TestCase1 the database raises a DuplicateKey exception because those IDs already exists in the database. And even saying the magic word "please" won't help with database duplicate key errors.
Good news is, there are multiple ways to solve this problem! Here are a couple...
Solution 1
Make sure if you are overriding the class method tearDownClass that you call super().tearDownClass(). If you override tearDownClass() without calling its super, it will in turn never call TransactionTestCase._post_teardown() nor TransactionTestCase._fixture_teardown(). Quoting from the doc string in TransactionTestCase._post_teardown()`:
def _post_teardown(self):
"""
Perform post-test things:
* Flush the contents of the database to leave a clean slate. If the
class has an 'available_apps' attribute, don't fire post_migrate.
* Force-close the connection so the next test gets a clean cursor.
"""
If TestCase.tearDownClass() is not called via super() then the database is not reset in between test cases and you will get the dreaded duplicate key exception.
Solution 2
Override TransactionTestCase and set the class variable serialized_rollback = True, like this:
class MyTestCase(TransactionTestCase):
fixtures = ['test-data.json']
serialized_rollback = True
def test_name_goes_here(self):
pass
Quoting from the source:
class TransactionTestCase(SimpleTestCase):
...
# If transactions aren't available, Django will serialize the database
# contents into a fixture during setup and flush and reload them
# during teardown (as flush does not restore data from migrations).
# This can be slow; this flag allows enabling on a per-case basis.
serialized_rollback = False
When serialized_rollback is set to True, Django test runner rolls back any transactions inserted into the database beween test cases. And batta bing, batta bang... no more duplicate key errors!
Conclusion
There are probably many more ways to implement a solution for the OP's issue, but these two should work nicely. Would definitely love to have more solutions added by others for clarity sake and a deeper understanding of the underlying Django test case base classes. Phew, say that last line real fast three times and you could win a pony!
The log you provided states DETAIL: Key (product_name)=(Almonds) already exists. Did you verify in your db?
To prevent such errors in the future, you should prefix all your test data string by test_
I discovered the issue, as noted at the bottom of the question.
From what I can tell, the database didn't like me using duplicate data in the setUpTestData() methods of two different tests. Changing the primary key values in the second test corrected the problem.
I think the problem here is that you had a tearDownClass method in your TestCase without the call to super method.
In this way the django TestCase lost the transactional functionalities behind the setUpTestData so it doesn't clean your test db after a TestCase is finished.
Check warning in django docs here:
https://docs.djangoproject.com/en/1.10/topics/testing/tools/#django.test.SimpleTestCase.allow_database_queries
I had similar problem that had been caused by providing the primary key value to a test case explicitly.
As discussed in the Django documentation, manually assigning a value to an auto-incrementing field doesn’t update the field’s sequence, which might later cause a conflict.
I have solved it by altering the sequence manually:
from django.db import connection
class MyTestCase(TestCase):
#classmethod
def setUpTestData(cls):
Model.objects.create(id=1)
with connection.cursor() as c:
c.execute(
"""
ALTER SEQUENCE "app_model_id_seq" RESTART WITH 2;
"""
)
This may be a really easy question but I've been looking for a while at django documentation and didn't find the answer.
My problem is that I want to check if, given a language, a user, who can speak multiple, speaks the given one.
My relevant classes:
class Language(models.Model):
idiom = models.CharField(max_length=40, unique=True)
class Profile(UserenaBaseProfile):
spoken_languages = models.ManyToManyField(Language, blank = True)
Given: query_set = Profile.objects.all()
I've tried things like:
ls = Language.get(idiom="some language here")
query_set.filter(spoken_languages__idiom__contains=ls.idiom)
query_set.filter(spoken_languages__contains=ls)
or
ls = Language.objects.filter(idiom="some language")
query_set.filter(spoken__languages__in=ls)
Some more but without success, it seems it should be quite easy but still I cannot find the correct approach. Any idea would be indeed appreciated.
This should work:
profiles = Profile.objects.filter(spoken_languages__idiom="language here")
Note that calling .filter() on a queryset does not change the queryset object. Instead, it creates and returns a clone with the new filters applied. So if you want to filter an existing queryset, you should do:
query_set = query_set.filter(spoken_languages__idiom="language here")
I'm trying to mimic Django's unique_together feature, but I can't seem to get it straight
class MyClass(ndb.Model):
name = 'name'
surname = 'surname'
phone = 'phone'
def get_unique_key(self):
return self.name + "|" + self.surname + "|" + self.phone
"Yeah, pretty easy" NOT
According to the accepted answer in this post, simply assigning the id param in the obj constructor was enough. But I don't want to handle that in a view. Ideally, I'd do this:
object = MyClass()
object = object.custom_populating_method(form.cleaned_data)
object.id = object.get_unique_key()
object.put()
Or even better, place that in a _pre_put_hook, so that the id would be set as last thing before saving (and maybe do some checking enforcing the uniqueness of the data across the datastore).
Apparently, I was wrong. The only way to achieve this is by hacking the view:
unique_id = "|" + form.cleaned_data['bla'] + "|" + form.cleaned_data ...
object = MyClass(id=unique_id)
which is awful and completely wrong (since every change to the model's requirements needs to be inspected also in the views). Plus, I'd end up doing a couple of ugly calls to fetch some related data.
I've spent too much time, probably, on this problem to see an exit and I really hope I'm missing something obvious here, but I can't really find any good example nor proper documentation around this subject. Has anyone any hint or experience with something similar?
tl;dr: is there a nice way to achieve this without adding unnecessary code to my views?
(I'm "using" Django and ndb's Models on the Datastore)
Thanks
Use a factory or class method to construct the instance.
class MyClass(ndb.Model):
name = ndb.StringProperty()
surname = ndb.StringProperty()
phone = ndb.StringProperty()
#staticmethod
def get_unique_key(name,surname,phone):
return '|'.join((name,surname,phone))
#classmethod
#transactional
def create_entity(cls,keyname,name,surname,phone):
key = ndb.Key(cls, cls.get_uniquekey())
ent = key.get()
if ent:
raise SomeDuplicateError()
else:
ent = cls(key=key, name=name,surname=surname,phone=phone)
ent.put()
newobj = MyClass.create_entity(somename, somesurname, somephone)
Doing it this way allows you to also ensure the key is unique by creatin the key and tring to fetch it first.
I think you can do it if you assign the entire key, rather than just the id:
object = MyClass()
object = object.custom_populating_method(form.cleaned_data)
object.key = ndb.Key(MyClass, object.get_unique_key())
object.put()