首页 > 解决方案 > 使用 Django 函数在数据库中添加一个字段

问题描述

这是我的模型课:

class complete_taxi_driver_account(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    STATUS_CHOICES = (('woman', 'Woman'), ('man', 'Man'),)
    first_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=40)
    phone_number = models.CharField(max_length=11)
    life_place = models.CharField(max_length=120)
    birth_date = models.DateTimeField(null=False, blank=False)
    gender = models.CharField(max_length=25, choices=STATUS_CHOICES,     default='man')
    number_plates = models.CharField(max_length=8)
    national_code = models.CharField(max_length=10)
    car_number = models.CharField(max_length=50)
    name_and_type_of_car = models.CharField(max_length=50)

    # Check if this car is taxi , this field will change to True else False
    confirmation = models.BooleanField(default=False)

    # If the taxi app starts, this section will change True, meaning the taxi is ready to pick up passengers
    online = models.BooleanField(default=False)

    #I do not know what to do here to add these two fields to the database when this function is called 
    def location(self):
        # Location of the taxi
        lat = models.DecimalField(max_digits=20, decimal_places=10)
        lng = models.DecimalField(max_digits=20, decimal_places=10)

我想在模型类中有一个函数,当被调用时,它会在数据库中创建两个字段并填充它们,然后通过调用它来更新它们。我怎样才能做到这一点?

标签: djangofunctiondjango-models

解决方案


推荐阅读