Sandeep Lade
Sandeep Lade

Reputation: 1943

setting Primary key in Django model

I have changed primary key in my model

class lab(models.Model):
    IP = models.CharField(max_length=250 , primary_key = True)
    PingStatus = models.CharField(max_length=250)
    SSHConnectivity = models.CharField(max_length=250)
    SSHLogin = models.CharField(max_length=250)
    DeviceType = models.CharField(max_length=250)
    DeviceVersion = models.CharField(max_length=500)

I am trying to make two entries by assigning two different "IP" values for lab object. But somehow only one object is there in model

>>> a=lab(IP="1.2.3.4")
>>> a=lab(PingStatus="1.2.3.4")
>>> a=lab(SSHConnectivity="1.2.3.4")
>>> a=lab(SSHLogin="1.2.3.4")
>>> a=lab(DeviceType="1.2.3.4")
>>> a=lab(DeviceVersion="1.2.3.4")
>>> a.save()
>>> lab.objects.all()
<QuerySet [<lab: lab object>]>
>>> a=lab(IP="1.2.3.5")
>>> a=lab(PingStatus="1.2.3.4")
>>> a=lab(SSHConnectivity="1.2.3.4")
>>> a=lab(SSHLogin="1.2.3.4")
>>> a=lab(DeviceType="1.2.3.4")
>>> a=lab(DeviceVersion="1.2.3.4")
>>> a.save()
>>> lab.objects.all()
<QuerySet [<lab: lab object>]>
>>> b=lab(IP="1.2.3.5")
>>> b=lab(PingStatus="1.2.3.4")
>>> b=lab(SSHConnectivity="1.2.3.4")
>>> 
>>> b=lab(SSHLogin="1.2.3.4")
>>> b=lab(DeviceType="1.2.3.4")
>>> b=lab(DeviceVersion="1.2.3.4")
>>> b.save()
>>> lab.objects.all()
<QuerySet [<lab: lab object>]>
>>> 

Can someone check ? Am I missing something here ?

Upvotes: 0

Views: 3736

Answers (2)

dghy
dghy

Reputation: 105

You should create instance of the class and later set values for that instance, for example:

a = lab(IP="1.2.3.4")
a.PingStatus = "1.2.3.4"
a.save()

Creating instance with all parameters set at once should help too:

b = lab.objects.create(...)

Upvotes: 1

Mark Chackerian
Mark Chackerian

Reputation: 23592

Try setting all of the values that you want at once, e.g.

b = lab.objects.create(value1='xx', value2='yy', value3='zz')

Also, you're not following conventions. Your model name should be capitalized, and your field names should be snake_case. For example, lab -> Lab, PingStatus -> ping_status

Upvotes: 0

Related Questions