0

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 .

  • let's say there is a company which gives offer on his(companies own site) site and a different offer on other site(like Amazon)
  • I want to store company details for which I did something as below.
  • Next I have created the Direct Offers
  • Last I have created Vendors offer
  • All the models are as below

    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.

1
  • you can have a Offers table only, and then can have boolean fields like for_vendors and for_direct? Commented Feb 18, 2022 at 7:50

1 Answer 1

1
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)
Sign up to request clarification or add additional context in comments.

1 Comment

A unique Offer model is the way to go. Implementation details can vary depending on your needs but the global approach Noah provided is good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.