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

On Wed, 8 May 2019 11:05 Robin Riis, <riis.som...@gmail.com> wrote:

> 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
>
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@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
> <https://groups.google.com/d/msgid/django-users/CAPLZMbPTNJ8OavQiaUpdELVH4wo9V9p94zkFWDwZS4sXdZtS8g%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CALJt1ntd4roJy_KOpXRDb9%2BfGLeue2gkJSQi5Q8cKXT3%2B8zk%2Bg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to