models

10 views
Skip to first unread message

Robin Riis

unread,
May 8, 2019, 6:35:15 AM5/8/19
to django...@googlegroups.com
if i have a model like

class ShelfManager(models.Manager):
use_for_related_fields = True

def create_cabinet(self, name, number_of_shelfs):
cabinet = self.create(name=name, number_of_shelfs=number_of_shelfs)
created_shelfs = 0
while created_shelfs < number_of_shelfs:
cabinet.shelfs.create()
created_shelfs += 1
return place

class Cabinet(models.Model):
name = models.CharField(max_length=50, unique=True)
number_of_shelfs = models.IntegerField
shelfs = ShelfManager()

class Shelfs(models.Model):
place = models.ForeignKey(Cabinet, on_delete=models.CASCADE, null=True, blank=True, related_name='shelfs')
...

and i want shelf to hold either model Box, Filter or Barrel
but not sure which approach to use :P


Mohammad Etemaddar

unread,
May 8, 2019, 7:34:17 AM5/8/19
to django...@googlegroups.com
The one who should have foreign key is is filter, box and barel to shelf.
Then you can access them from shelf:
Shelf0.boxs
Shelf0.barels
And shelf0.filters

Note that you can set the relation_name in foreign key so that you can use the related name instead of filters, boxs and barels

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPLZMbPTNJ8OavQiaUpdELVH4wo9V9p94zkFWDwZS4sXdZtS8g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages