A
A
Alexey Chernov2015-11-24 17:40:56
Django
Alexey Chernov, 2015-11-24 17:40:56

How to bind User model to posts?

class User(models.Model):
    user_first_name = models.CharField(max_length=25)
    user_lost_name = models.CharField(max_length=25)
    user_old = models.IntegerField(default=0)
    reg_date = models.DateTimeField('date registration')

    def __str__(self):
        return self.user_first_name

class Post(models.Model):
    <b>Label = models.ForeignKey(User)</b>
    Text = models.CharField(max_length=500)
    Type = models.CharField(max_length=90)
    Date = models.DateTimeField(auto_now_add=False)
    Views = models.IntegerField(default=0)
    Comments = models.IntegerField(default=0)
    def __str__(self):
        return self.Text

did so, but gives an error "no such column: doski_post.Label_id"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2015-11-25
@syschel

1. Your keyboard would be taken away for capital letters in variable names. Be sure to read pep8 !
2. Run the migration

python manage.py makemigrations
python manage.py migrate

3. Where did you get such a User model from? There is a basic one with the same name, but you have it not redefined according to the rules. No problem catching errors on name conflicts.
4.
“Write code as if it were accompanied by a violent psychopath who knows where you live.” © Steve McConnell

A
Alexandra Vorontsova, 2016-04-21
@alexandret

Here is an example of what you want to do as I understand it.
Pay attention to the line author = models.ForeignKey('auth.User')
It says that the post will be linked to one of the users registered on the site.
Then you can simply edit the auth.User model and expand it as you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question