This is a fun one :-)
Working on an EAV, we inject a generic relationship handler at runtime in a model.
model_cls is any class, and a EavValue class have a generic relation pointing to it. It works fine from EavValues to a model_cls, but on the other way we need to inject an accessor to ease things:
generic_relation = generic.GenericRelation(EavValue,
object_id_field='entity_id',
content_type_field='entity_ct',
related_name=model_cls.__name__)
generic_relation.contribute_to_class(model_cls, 'eav_values')
Again, we do that at runtime because we want to make it work with untouchable 3rd party libs.
While unittesting with a Patient class as model_cls, we get the following error:
eav_ng.patient: Accessor for m2m field 'eav_values' clashes with related m2m field 'EavValue.Patient'. Add a related_name argument to the definition for 'eav_values'.
Now, we thought the easy fix was to change either the second parameter of contribute_to_class or related_name in GenericRelation, but it doesn't! We get exactly the same error, only with different name.
Second strange thing, running the same unittests with Sqlite instead of MySql: all pass.
What's more, no matters the order or the tests, we always get this error at the second tests. Since this process happen in a register method and that we call register and unregister at setup and tear down, I'm guessing our unregister method is imperfect.
Last strange fact: we get the error while running unittest, but we are unable to reproduce it manually. Worst, on my colleague computer, it doesn't get the error while we are using the same version of Python, Django, Ubuntu and MySQL.
We solved a lot of hard ones but we are kind of stuck on this one so any clue appreciated.
UPDATE:
New clues for this great game:
Errors are raised from this snippet in django.core.management.validation, on line 245 (django 1.2.1):
for r in rel_opts.get_all_related_many_to_many_objects():
if r.field is not f:
if r.get_accessor_name() == rel_name:
e.add(opts, "Accessor for m2m field '%s' clashes with related m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
if r.get_accessor_name() == rel_query_name:
e.add(opts, "Reverse query name for m2m field '%s' clashes with related m2m field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
For us r.get_accessor_name() == rel_name is True, as both are "Patient".
UPDATE 2:
When we add an app that register a model. Any model, the problem doesn't appear anymore. So much for the unregister theory...
We at two symmetrical errors (both sides of the relation). Removing related_name suppress one of the errors 0_o
Found the solution
Adding a generic relation on put a reference in the model class _meta.local_many_to_many attribute which is a list. Django check against that but provides no way to get rid of it. Fix is:
# remove remaining reference to the generic relation
for field in model_cls._meta.local_many_to_many:
if field.name == 'eav_value': # your related name
model_cls._meta.local_many_to_many.remove(field)
break
Related
I have the following QuerySet:
MyModel.objects
.order_by("foreign_key_id")
.distinct("foreign_key_id")
.in_bulk(field_name="foreign_key_id")
foreign_key_id is not unique on MyModel but given the use of distinct should be unique within the QuerySet.
However when this runs the following error is raised:
"ValueError: in_bulk()'s field_name must be a unique field but 'foreign_key_id' isn't."
According to the Django docs on in_bulk here it should be possible to use in_bulk with distinct in this way. The ability was added to Django in response to this issue ticket here.
What do I need to change here to make this work?
I'm using Django3.1 with Postgres11.
As the documentation of in_bulk(…) says:
(…)
Changed in Django 3.2:
Using a distinct field was allowed.
Since you use django-3.1, this will thus not work, you will thus have to upgrade your program to django-3.2.
I'm trying to put a placeholder in a Select2 field, by putting empty_label in the field. Now i get the error:
TypeError: __init__() got multiple values for keyword argument 'empty_label'
Here is my code:
self.fields['work_types'] = ModelMultipleChoiceField(
queryset=WorkTypes.objects.filter(
white_label_client=logged_in_user.white_label_client,
deleted=False),
empty_label='Type',
required=False)
What do i have to change?
According to the comments here, it is a current bug in Django. One of the users said:
"Looking at it, I would say that this is by design. You shouldn’t use an empty label on a multiple choice field. Perhaps this should be clarified in the docs.
Saying that, if you really want an empty label on a multiple choice field, you could create your own class that initializes empty_label however you want it."
Further explanation
"Well, it is raising an error. It’s a Python error. Basically, you were passing a keyword argument (empty_label), and the class was passing the same keyword argument called empty_label. Expanded out, this is what was happening:"
field = forms.ModelMultipleChoiceField( queryset = Imodelclass.objects.all(), empty_label='------------' ) = ModelMultipleChoiceField(queryset= Imodelclass.objects.all(), empty_label=None, empty_label='------------')
"When you read the error, you see that is actually very clear in this context: You passed two arguments for the same keyword."
"I think it would be wise to mention this in the documentation; perhaps even providing a rationale why ModelMultipleChoiceField should not have an empty label. But if there is no rationale behind it, then the clear solution is to do exactly as you did: remove the empty_label keyword argument from the super call."
Recently I found out that its possible to define Django form validation directly in the models.py file. This can be done the following way:
fev1_liter = models.DecimalField(validators=[MaxValueValidator(8.2),
MinValueValidator(0.3)],
max_digits=3, decimal_places=2)
This is an awesome alternative to validation in forms.py, but I do have a very annoying problem:
How can I control in which order the validation is executed?
In this example Django will first validate if the inputs digits is in the format x.xx and thereafter min and max value. This results in some very confusing error messages.
Thanks in advance!
For each model field, field.clean() first performs field validation via field.validate(), then via field.run_validators(), validators are called in order they are returned from the field.validators iterator.
This makes sense, because in the general case you can expect your validators to fail if the field validation failed, so it makes for easier debugging. Remember that field validators are non-obligatory, so field.validate() takes precedence. If you want to change the behavior, you'll have to create your own Field classes and override the field.clean() behavior.
You can inspect the field sources for more details.
I'm fairly new to django web development. And I got an error whereby I try to change a 'post' under admin url - so localhost:8080/admin. I'm able to create it successfully but when I try to click the post that I had just added. I'm getting this error:
Exception Type: DatabaseError Exception Value: This query is not
supported by the database.
And this is the code that I know is 'messing' with this query:
#Post is an abstract class
class BlogPost(Post):
...
translators = models.ManyToManyField(Staff, related_name='translators')
photographers = models.ManyToManyField(Staff, related_name='photographers')
authors = models.ManyToManyField(Staff, related_name='authors')
...
To explain what is going on with this blog post - it can have multiple 'owners'/people that contributed to this post and thus the decision using ManyToManyField. And vice-versa with the 'Staff' member - the type of 'member' can have multiple ownership on multiple posts (Let me know if this logic doesn't make any sense because it does to me).
I'm using mongodb for the database, django 1.5.11 and I have installed djangotoolbox. I've tried the following solutions with adding a relationship to BlogPost as shown below:
Class Staff(Member):
...
staff_posts = models.ManyToManyField(BlogPost, related_name="staff_posts")
...
But I'm getting an error on 'cannot import BlogPost'. I tried figuring out the reason of this error and I don't think that I have a circular dependance - after checking all of the files, there's no circular dependance.
MongoDB (or mongoengine, which I'm guessing you're using) doesn't support joins, so the typical way to model many-to-many relations in a relational database has to be implemented some other way.
One way is to use a ReferenceField inside a ListField. It might look like this (not tested):
class BlogPost(Post):
authors = models.ListField(models.ReferenceField(Staff))
...
Also see these answers:
https://stackoverflow.com/a/18747306/98057
https://stackoverflow.com/a/25568877/98057
Just to put it out there, I'm not real familiar with MongoDB.
However, I don't believe you need to define a ManyToManyField on your Staff class. You already have a ManyToMany defined in your BlogPost, having it defined in one class file is all that is required. (At least for MySQL).
This is a follow-up question on Delete field from standard Django model. In short: a field can be dynamically deleted from a model that is already created, in this case the field User.email . So field email would be deleted from User without changing the code for User. See code below for example.
I can dynamically delete a a field from a model(1), but that happens when the server starts and is undone when it exists. Since syncdb doesn't require the server to be running, and generally seems to ignore the deletion code (somehow), this approach doesn't prevent the field from appearing in the database(2).
Is there a way to do delete the field from the model (without changing the file it's in, as it's a Django model), in a way that also makes it not appear in the database?
Thanks in advance!
Mark
EDIT: I problem is not that I am deleting the text "m = models.IntegerField()" from the model file and want the field removed from the database. The problem is that I am using the code below to remove a field from a model that has already been declared in another file. I do not think that creating a migration with South for every time I run syncdb is a solution(3).
Additional information:
1) Currently, the code is in models.py, but I suppose Where to put Django startup code? works.
2) I can delete it on post_syncdb signal with custom query, but I hope for something more elegant... Or elegant at all, more accurately.
3) If it even works at all, because obviously syncdb is still seeing the 'removed' field), so I think South would to as it's still somehow there.
This is the code (models.py):
class A(models.Model):
m = models.IntegerField()
for i, f in enumerate(A._meta.fields):
if f.name == 'm':
del A._meta.fields[i]
break
class B(A):
n = models.IntegerField()
for i, f in enumerate(B._meta.fields):
if f.name == 'm':
del B._meta.fields[i]
break
EDIT: I checked (with print) and the deletion code is executed on syncdb. It is executed before tables are created
django does a lot of meta class magic and i would guess that the meta class is responsible for defining the database table to back your model. Subsequently just deleting the field is not enough to alter the generated table.
as several people have pointed out, south is the way to deal with these problems.