Reputation: 13
I made some model classes with pets types: Parrot, Rabbit, Cat, Dog and i need make model Shop, where one field will be related for few of this models. This field will show what sells in shop. Can i make relation beetween one model object to few model classes?
If i can't, how i need change my scheme?
ex:
1 Shop1 [Parrot, Rabbit]
2 Shop2 [Cat, Dog, Rabbit]
Upvotes: 1
Views: 151
Reputation: 125139
If I understand your question correctly, you have created model classes representing different types of pets, and you want to associate individual shops with a number of those classes.
I'm assuming your pet model definitions look something like this:
class Parrot(models.Model):
pining_for_fjords = models.BooleanField(default=True)
You could do this using contenttypes
:
from django.contrib.contenttypes.models import ContentType
class Shop(models.Model):
pet_types = models.ManyToManyField(ContentType)
parrot_type = ContentType.objects.get(app_label="myapp", model="parrot")
// ManyToManyFields need to be added to already saved models
my_shop = Shop.objects.create()
my_shop.pet_types.add(parrot_type)
Upvotes: 3