Home > Back-end >  how to create foreign key from multiple models on single field in django
how to create foreign key from multiple models on single field in django

Time:09-29

i have multiple models like,

class Service1(models.Model):
   price = models.FloatField()
   description = models.TextField()

class Service2(models.Model):
   price = models.FloatField()
   monthly_binifit = models.CharField()
   description = models.TextField() 

class Service3(models.Model):
   price = models.FloatField()
   free_contact = models.IntegerField()
   monthly_binifit = models.CharField()
 
class Service4(models.Model)
   price = models.FloatField()
   monthly_binifit = models.CharField()
   description = models.TextField()
   other_binifit = models.CharField()

class Orders(models.Model):
     user = models.ForeignKey(User, on_delete=models.CASECAD)
     orders = models.ForeignKey((Service1, Service2, Service3, Service4), on_delete=models.CASECAD)

how is this possible to create orders with different services like above

CodePudding user response:

I don't fully understand what you're trying to do without all the details but why can't you just have 'Services' as a foreign key and have Service1, Service2, Service3, Service4 as objects in objects in Services?

    class Services(models.Model)
        service = (
        ('Service1', ('Service1')),
        ('Service2', ('Service2')),
        ('Service3', ('Service3')),
        ('Service4', ('Service4')),
    )

    name = models.CharField(choices=service)


    class Orders(models.Model)
        orders = models.ForeignKey(Services,)

CodePudding user response:

The best option I come with is to set the foreign relationships in the opposite direction:

class Service1(models.Model):
   price = models.FloatField()
   description = models.TextField()
   order = models.ForeignField(Order, on_delete=models.PROTECT, related_name='services1')

class Service2(models.Model):
   price = models.FloatField()
   monthly_binifit = models.CharField()
   description = models.TextField() 
   order = models.ForeignField(Order, on_delete=models.PROTECT, related_name='services2')

class Service3(models.Model):
   price = models.FloatField()
   free_contact = models.IntegerField()
   monthly_binifit = models.CharField()
   order = models.ForeignField(Order, on_delete=models.PROTECT, related_name='services3')
 
class Service4(models.Model)
   price = models.FloatField()
   monthly_binifit = models.CharField()
   description = models.TextField()
   other_binifit = models.CharField()
   order = models.ForeignField(Order, on_delete=models.PROTECT, related_name='services4')

class Orders(models.Model):
   user = models.ForeignKey(User, on_delete=models.CASECAD)

This way you can query for the services related to an order, one type at a time:

order = Order.objects.get(id=1)
services_1 = order.services1.all()
services_2 = order.services2.all()
services_3 = order.services3.all()
services_4 = order.services4.all()

CodePudding user response:

You can't. But you can filter the objects in your function to reach the a specific class object.

  • Related