0

I am new to using django and would appreciate your input to the following problem.

Two models are present, namely A and B. Instances of A must belong to at least one instance of B, while a B could contain any number of instances of As from 0 to infinite.

class A(models.Model):
  name = models.CharField(max_length = 128)
  created_date = models.DateTimeField(auto_now_add = True)
  modified_date = models.DateTimeField(auto_now = True)

class B(models.Model):
  name = models.CharField(max_length = 128)
  created_date = models.DateTimeField(auto_now_add = True)
  modified_date = models.DateTimeField(auto_now = True)
  A_list = models.ManyToManyField(A)

Okay. I think, based on the description I have given, ManyToManyField should be used for the two models. The thing is, from what I have read, blank = false must be used to prevent a null assignment for A (since an A must be assigned to at least one B) but a B may not have an assigned A (and thus should not have the blank = false statement. Could anyone asymmetric ManyToMany relation can be implemented in django Model?

1 Answers1

0

You should place the ManyToManyField in A, with null=False, and blank=False (default, you got this right). This way, you can create B instances with no problems, even when there are no existing A instances.

When you create an instance of A, blank=False makes sure the B_list field is required. The null=False makes sure the database will not allow NULL values.

Your code would become:

class A(models.Model):
  name = models.CharField(max_length = 128)
  created_date = models.DateTimeField(auto_now_add = True)
  modified_date = models.DateTimeField(auto_now = True)      
  B_list = models.ManyToManyField('B')

class B(models.Model):
  name = models.CharField(max_length = 128)
  created_date = models.DateTimeField(auto_now_add = True)
  modified_date = models.DateTimeField(auto_now = True)

For the difference between null and blank in Django, see this post:

differentiate null=True, blank=True in django

Community
  • 1
  • 1
Jorick Spitzen
  • 1,559
  • 1
  • 13
  • 25
  • I understand the logic. So should there be ```blank = False``` in ```B_list = models.ManyToManyField('B')```, or should it not? Also, from what I have read, the argument to ```ManyToManyField``` was the pointer to the class, not a string value, in this case ```'B'``` in ```ManyToManyField('B')```. Is there something that I am missing or is that a typo? –  May 12 '15 at 23:18
  • To your first question, no you don't need `blank = False` since this is the default. You could put it in there, but it wouldn't change anything. The string value is needed here because your A model is defined above your B model. At that point in the code, the B model does not exist yet, and the string parameter makes sure it is evaluated when all the models are ready. You could also switch them around (put `A` below `B` and simply say `ManyToManyField(B)`) – Jorick Spitzen May 13 '15 at 07:05
  • It did not occur to me the ordering in Python code was relevant. I ended up switching the order of the two classes last night. Thank you. –  May 13 '15 at 18:07