Issue
I am in need to create two auto generated field: 1st field is ID and another i am taking position that is equivalent to id or we can say it is also auto generated field in the model.
here is the code in which i am integrating:
class DeviceControlPolicy(models.Model):
vendor_id = models.ForeignKey(Vendor, on_delete=models.CASCADE)
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
# schedule_id = models.ForeignKey(ScheduleClassificationGroup, on_delete=models.CASCADE, null=True, default=None)
usb_storage_device = models.CharField(max_length=10, default="allow")
cd_dvd = models.CharField(max_length=10, default="allow")
portable = models.CharField(max_length=10, default="allow")
wifi = models.CharField(max_length=10, default="allow")
bluetooth = models.CharField(max_length=10, default="allow")
webcam = models.CharField(max_length=10, default="allow")
serial_port = models.CharField(max_length=10, default="allow")
usb_port = models.CharField(max_length=10, default="allow")
local_printer = models.CharField(max_length=10, default="allow")
network_share = models.CharField(max_length=10, default="allow")
card_reader = models.CharField(max_length=10, default="allow")
unknown_device = models.CharField(max_length=10, default="allow")
position = model.[what do i write here to make it auto generated or equal to id]
def __str__(self):
return self.name
please help me out to solve this.
Solution
You can override the save
method to set the initial value of position
:
class DeviceControlPolicy(models.Model):
vendor_id = models.ForeignKey(Vendor, on_delete=models.CASCADE)
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
# schedule_id = models.ForeignKey(ScheduleClassificationGroup, on_delete=models.CASCADE, null=True, default=None)
usb_storage_device = models.CharField(max_length=10, default="allow")
cd_dvd = models.CharField(max_length=10, default="allow")
portable = models.CharField(max_length=10, default="allow")
wifi = models.CharField(max_length=10, default="allow")
bluetooth = models.CharField(max_length=10, default="allow")
webcam = models.CharField(max_length=10, default="allow")
serial_port = models.CharField(max_length=10, default="allow")
usb_port = models.CharField(max_length=10, default="allow")
local_printer = models.CharField(max_length=10, default="allow")
network_share = models.CharField(max_length=10, default="allow")
card_reader = models.CharField(max_length=10, default="allow")
unknown_device = models.CharField(max_length=10, default="allow")
position = models.IntegerField(blank=True, null=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if self.position == None:
self.position = self.id
# You need to call save two times since the id value is not accessible at creation
super().save()
Answered By - RedWheelbarrow
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.