Sachin Rajput
Sachin Rajput

Reputation: 248

Django database modeling

I have a scenerio for which im trying to create the database models but I'm not able to find a perfect way to do it Scenerio is as follows .


    class Company(models.Model):
        name= models.CharField(max_length=50, null=True)
        address= models.CharField(max_length=250, null=True)
    
    class DirectOffers(models.Model):
        Smartphone= models.FloatField(max_length=50, null=True)
     
    class VendorsOffers(models.Model):
        Smartphone= models.FloatField(max_length=50, null=True)
        category = models.CharField(max_length=250, null=True)
        owner=models.ForeigKeyField("Company",on_delete=models.CASCADE)

But the above doesn't seems right So any help or guidance will be a great help.

Upvotes: 0

Views: 40

Answers (1)

Vi6hal
Vi6hal

Reputation: 361

class Company(models.Model):
    name= models.CharField(max_length=50, null=True)
    address= models.CharField(max_length=250, null=True)

class Offer(models.Model):
    owner=models.ForeigKeyField("Company",on_delete=models.CASCADE)
    Smartphone= models.FloatField(max_length=50, null=True)
    category = models.CharField(max_length=250, null=True)
    vendor_only = models.BooleanField(default=False)
    # company flag denotes that the offer is only applicable on the company site.
    company_only = models.BooleanField(default=False)
    # this will create another table `vendor_offers` to store the details of the vendors for which a certain offer is applicable  
    vendors = models.ManytoManyField("Vendors")

This also makes Querying easy

#getting offers only for a specific vendor    
Offer.objects.filter(vendors__vendor=21)
#getting offers only for vendors    
Offer.objects.filter(vendor_only=True)
#getting offers which are available both places     
Offer.objects.filter(vendor_only=True,company_only=True)

Upvotes: 1

Related Questions