Home > database >  Need to make two model fileds as primary key
Need to make two model fileds as primary key

Time:03-24

Here profile and category should be primary key how can i do this.

class QuickApproximation(models.Model):
        profile = models.CharField(primary_key=True, null=False, max_length=1024, default=None)
        category = models.CharField(null=False, max_length=1024, default=None)
        approximation = models.IntegerField(null=False, default=None)

I need to dump this data into DB

Profile,Category,Approximations
Always on road,Groceries,500
Always on road,Dining,500
Always on road,Gas,500
Always on road,Others,1500
Social Butterfly,Groceries,500
Social Butterfly,Dining,500
Social Butterfly,Gas,500
Social Butterfly,Others,1500
Foodie,Groceries,500
Foodie,Dining,500
Foodie,Gas,500
Foodie,Others,1500
Pantry Stocker,Groceries,500
Pantry Stocker,Dining,500
Pantry Stocker,Gas,500
Pantry Stocker,Others,1500
Avid Shopper,Groceries,500
Avid Shopper,Dining,500
Avid Shopper,Gas,500
Avid Shopper,Others,1500
Healthy nut,Groceries,500
Healthy nut,Dining,500
Healthy nut,Gas,500
Healthy nut,Others,1500
Digital Junky,Groceries,500
Digital Junky,Dining,500
Digital Junky,Gas,500
Digital Junky,Others,1500

and everything should be available in db

CodePudding user response:

I think you must give primary_key to it field which you want. I suggest you read this here: https://docs.djangoproject.com/en/4.0/topics/db/models/

CodePudding user response:

Django does not allow compound primary keys. So you can only give the primary_key=True attribute to one field.

  • Related