Is there a OneToManyField relationship in Django? There is a ManyToOneField relationship but that restrict you declare the relationship on the Many side.
You should recognise that Django fields represent database columns. A ForeignKey field is exactly that, a field on the model that represents a key in another model. But you can't model a "one-to-many" field in that way; what would the field on the model represent? So no, it is not possible.
Related
I have a model. I want a list of models which have foreign key (or any) relation with my given model. please help.
In case you have a ForeignKey relation, then Django makes a relation in reverse as well. You thus can inspect the Options object [Django-doc]. We thus can obtain a list of fields with:
[field for field in MyModel._meta.get_fields() if field.is_relation]
you can obtain the model to which these are linked by accessing the .related_model attribute [Django-doc]:
[field.related_model for field in MyModel._meta.get_fields() if field.is_relation]
I am curious how Django handles model relationships at the object level because I am working on building a custom json serializer, and I need to understand this so I have properly handle nested serialization. I am almost positive I will have to dive into some of the internals of python, but that will not be too big of a deal.
The field name in the model has _id appended to it in the table, and it stores the PK of the foreign model (as a FK normally would).
When the related field is accessed on a model, Django performs a query to retrieve the foreign model from the database.
When a model is assigned to the related field, Django reads the PK of the model and assigns it to the backing field in the table.
I have two models and want to set a relation to them.
class ModelA(models.Model):
id = models.IntegerField(primary_key=True) # DB => PK, AI, NN
name = models.CharField(max_length=50)
...
class ModelB(models.Model):
modelA = models.OneToOneField("ModelA", primary_key=True)
description = models.CharField(max_length=255)
...
So I have a relationship between the two models. Is it possible to add a member to ModelA which stores the relation to ModelB without saving this relation to the database?
I would call it a dynamically created relation or something. Any hints oder suggestions how to let both models know each other?
I think it would be benefiting if the relation on one model can be done dynamically. Otherwise I'll get some trouble storing the models because one of the IDs won't be stored if I save one of the models.
I want to have the relation on both models so I can easily use the models as inline in django-admin.
regards
The reverse relation in Django is created by default.
To get the ModelA you will use:
ModelA.objects.filter(modelb__pk = 1)
You will find more details here:
https://docs.djangoproject.com/en/dev/topics/db/queries/
Django ORM will save ModelA first, then ModelB, in order to maintain data integrity in the DB.
Django can try saving multiple items in one transaction, and this way, if you cancel it, nothing will be saved, but this is possible in shell or in Python code. Over HTTP you can't maintain a transaction over several queries so far.
If you need to show model A as inline of model B, you need a custom admin interface, not new fields/models. I can't tell how to write custom admin widgets. I did do a similar thing with custom editor views & templates & Javascript. I stored the unsaved models in request.session.
I want to design a Task model that can be associated to Project Models, another X model, and other Task models. The obvious choice is a foreign key, but I want any particular instance of that Task model to only be associated to only one of those model types, that is, if a Task model has a relationship with a Project model, it cannot have a relationship with another Task model, and so on. Any advice in what would be the best way to represent this? Thanks.
Have a look at Generic relation. It lets you define a foreign key on multiple models. This way your task is only linked to one of your models.
What I have done is to inherit from a base class on all my models that will be related to tasks. Task models points to that base class on the ForeignKey with unique=True, and it seems like all subclasses inherit the relationship. Thanks.
I've always been using ForeignKeys.
A one-to-one relationship is a unique relation between two entities in both directions. I.e. for an entity A there exists only one entity B and vice versa.
The documentation says:
Conceptually, this is similar to a ForeignKey with unique=True, but the "reverse" side of the relation will directly return a single object.
This is most useful as the primary key of a model which "extends" another model in some way; Multi-table inheritance is implemented by adding an implicit one-to-one relation from the child model to the parent model, for example.