I have this relation defined in x\research.py file.
research_groups = models.ManyToManyField(ResearchGroup, related_name='researchs', blank=True,
through='ResearchResearchGroups')
Then i want copy it to y\research.py file with
research.research_groups.set(Research.research_groups.all())
And while im trying to use it i get
'ManyToManyDescriptor' object is not callable
How can i handle assigning m2m relation to another place?
Related
I noticed while debugging a Django application that with two tables with a 1:1 relation, trying to access the related object from the table that defines the 1:1 relation would return None, while trying to access the same nonexistent relation from the table that does not define the relation Django will raise a Model1.model2.RelatedObjectDoesNotExist exception. I was confused by this behavior as the same action has two different results on objects on the same type of relation.
This is an example of the models used:
class RelatedModel(models.Model):
pass
class RelationshipDefiner(models.Model):
related_object = models.OneToOneField(
RelatedModel,
on_delete=models.CASCADE,
related_name="relationship_definer",
blank=True,
null=True
)
This is an example of trying to print the relations to screen:
relationship_definer = RelationshipDefiner.objects.create()
related_object = RelatedModel.objects.create()
print("RELATIONSHIP DEFINER'S RELATED OBJECT:")
print(relationship_definer.related_object)
print("RELATED OBJECT'S RELATIONSHIP DEFINER:")
print(related_object.relationship_definer)
And the output:
RELATIONSHIP DEFINER'S RELATED OBJECT:
None
RELATED OBJECT'S RELATIONSHIP DEFINER:
Internal Server Error: /test/
File "/django/app/views/test.py", line 17, in test
print(related_object.relationship_definer)
app.models.sample.RelatedModel.relationship_definer.RelatedObjectDoesNotExist: RelatedModel has no relationship_definer.
Why do 1:1 nullable relationships behave differently on either side of the relation, and is there a way to coerce Django to behave consistently across both sides of the relationship?
Hey i need to create a model from form. It's different way if i need to create a some another model, that have a relations with created object model from form. its must work like that -> i come on site, get form for create a model object and save new object. an then -> i have another model with relations with model from form. and i need, to create relation model object automaticly - when django taked new object from forms.
tried any can help me. i make it this way, but this time i have problem. -> i have manytomany field in my relation model, and i have manytomany field ->users from form. and i cant create a relation model with this instance :( Traceback:
TypeError at /ru/center/add/
Direct assignment to the forward side of a many-to-many set is prohibited. Use users_center.set() instead.
but i tired to try it(( help please, how i may do it?
views.py
for x in form['users_center'].data:
name_center = form['name_center'].data
fullname_center = form['fullname_center'].data
ogrn = form['ogrn_center'].data
head_center = form['head_center'].data # user id
many_users = form['users_center'].data
user_center = x # user id
new_center = Center.objects.create(name_center=name_center,
fullname_center=fullname_center,
ogrn_center=ogrn,
head_center=User.objects.get(id=head_center),
users_center=User.objects.get(id=int(x)))
new_center.save()
models.py
users_center = models.ManyToManyField(User,
related_name='center',
# through=CenterDetails,
default=None, blank=True,
verbose_name=_("Сотрудники"))
There is a join table implied by the many-to-many relationship between two models. The error is letting you know that you must set the relationship after creating a User object instead of trying to assign the relationship while creating the User object. You can use set() or add() to do this.
So try doing:
new_center = Center.objects.create(
name_center=name_center,
fullname_center=fullname_center,
ogrn_center=ogrn,
head_center=User.objects.get(id=head_center),
)
users_center=User.objects.get(id=int(x))
new_center.users_center.add(users_center)
Additionally it may be useful to rename your many to many field as a plural to indicate the relationship. Maybe users_centers instead. Since it seems like users can have many centers, and centers can have many users. That's up to you though, not required for it to work.
I have started using pymodm in my project (https://github.com/ypriverol/biocontainers-backend/blob/master/biocontainers/common/models.py). All CRUD operations work very well.
However, when I try to retrieve the list of objects for a specific MongoModel using the objects call:
tool_versions_dic = MongoToolVersion.objects.all()
I got this error:
AttributeError: type object 'MongoToolVersion' has no attribute 'objects'
So I have the following
b = Brand.objects.create(**model_fields_and_values)
b.save()
and then I try to associate that entry with
b._meta.get_field("myfield").add(value3)
and I get the error 'ManyToManyField' object has no attribute 'add'
How can I create an association using a string and not the field ???
I dont want to use b.myfield.add(value3)
getattr allows you to get an attribute using the attribute name:
getattr(b, 'myfield').add(value3)
I have a django project which has two apps, one is AppA and AppB. Now AppA has a model
ModelA which is referenced by the model ModelB in AppB, using modelA = models.ForeignKey(ModelA, related_name='tricky')
Now in my view for AppA, when it shows ModelA, I do a get_object_or_404(ModelA, pk=prim_id). Then I want to get all the ModelBs which have a Foreign Key pointing to ModelA.
Documentation says I should do a mb = ModelB.objects.get(pk=prim_id) then mb.modela_set.all()
But, it failed on the mb.modela_set, and it says "ModelB object has no attribute 'suchsuch'". Notice I added the related_name field to ForeignKey, so I tried with that as well, including mb.tricky.all() and mb.tricky_set.all() to no avail.
Oh, and I have specified a different manager for AppA where I do objects = MyManager() which returns the normal query but with a filter applied.
What could be the problem? What is the prefered way to get the ModelBs referencing ModelA?
If the ForeignKey is, as you describe in ModelB and you do mb = ModelB.objects.get(pk=prim_id) then the look up for the modela attribute is not a reverse lookup. you simply access the related object via mb.modela!