[Answered ]-Django model field with database column

1👍

The constructor will work if you do:

class Stuff(models.Model):

    id = fields.UUIDField(primary_key=True)
    name = models.CharField(max_length=128)
    enabled = models.BooleanField(default=False)
    log_user = None

    def __init__(self, log_user=None, *args, **kwargs):
        super(Stuff, self).__init__(*args, **kwargs)
        self.log_user = log_user

1👍

You need ForeignKey here.

class Stuff(models.Model):
    id = fields.UUIDField(primary_key=True)
    name = models.CharField(max_length=128)
    enabled = models.BooleanField(default=False)
    log_user = models.ForeignKey('auth.User', blank=True, null=True)

0👍

Django recommend to use as a Foreign key Concept:

class Stuff(models.Model):
    id = fields.UUIDField(primary_key=True)
    name = models.CharField(max_length=128)
    enabled = models.BooleanField(default=False)
    log_user = models.ForeignKey('auth.User', blank=True, null=True)

while saving your Data you can add log_user object:
     Stuff = Stuff()
     stuff.log_user = user
     stuff.save()

Leave a comment