2👍
When creating a top level object, that contains other objects, it is generally a good idea to start with the entity that is the LEAST dependent on other objects.
Lets assume you have a model like this:
from django.db import models
class Mall(models.Model):
name = models.CharField(max_length=200)
def __unicode__(self):
return u"%s" % (self.name)
class Shop(models.Model):
name = models.CharField(max_length=200)
mall = models.ForeignKey(Mall)
def __unicode__(self):
return u"%s" % (self.name)
class Employee(models.Model):
name = models.CharField(max_length=200)
hired_by = models.ForeignKey(Shop)
def __unicode__(self):
return u"%s" % (self.name)
It is clear from this example that creating a Mall objects, is not dependent on any other class – because it does not have a ForeignKey to these. So we should start by creating a Mall object in our database:
>>> from myapp.models import Mall, Shop, Employee
>>> m = Mall(name="Super Mall")
>>> m.save()
>>> Mall.objects.all()
[<Mall: Super Mall>]
Now that we have a Mall we should be able to create objects that are only dependent on having a Mall object available. Shop has a ForeignKey to Mall and Employee has a ForeignKey to Shop. Since we have no Shop objects yet, we need to create a Shop object before we can create any Employee objects:
>>> s1 = Shop(name="First Shop", mall = Mall.objects.get(name="Super Mall"))
>>> s2 = Shop(name="Second Shop", mall = Mall.objects.get(name="Super Mall"))
>>> s1.save()
>>> s2.save()
>>> Shop.objects.all()
[<Shop: First Shop>, <Shop: Second Shop>]
At this point, the one-many relationship has already been established automatically in Django. This means we can type the following now:
>>> Mall.objects.get(name="Super Mall").shop_set.all()
[<Shop: First Shop>, <Shop: Second Shop>]
The only thing missing now is the Employee objects for each shop:
>>> e1 = Employee(name="Foo", hired_by=Shop.objects.get(name="First Shop"))
>>> e2 = Employee(name="Bar", hired_by=Shop.objects.get(name="First Shop"))
>>> e3 = Employee(name="John", hired_by=Shop.objects.get(name="Second Shop"))
>>> e4 = Employee(name="Carl", hired_by=Shop.objects.get(name="Second Shop"))
>>> e1.save()
>>> e2.save()
>>> e3.save()
>>> e4.save()
>>> Employee.objects.all()
[<Employee: Foo>, <Employee: Bar>, <Employee: John>, <Employee: Carl>]
Just like before the one-to-many relationship has been set up automatically between Employee and Shop and we can get all employees for a specific shop like this:
>>> s = Shop.objects.get(name="First Shop")
>>> s.employee_set.all()
[<Employee: Foo>, <Employee: Bar>]
At this point we can also chain access to the database from a top level manner – if you want to get all the employees from a specific shop, at a specific mall you can do this:
>>> Mall.objects.get(name="Super Mall").shop_set.get(name="Second Shop").employee_set.all()
[<Employee: John>, <Employee: Carl>]
I hope this makes it clear how to establish relationships, and the order in which you should create your objects.
So in your example you would first create an object of ModelA (similar to my Mall). Then you would create an object of ModelB using your existing ModelA object (like I create my Shop), and then you can finally create objects of ModelC and ModelD using your ModelB object (like I do with my Employee).
Feel free to comment if you are unsure about something.