here is the error:
>>> from blog.models import Post
>>> user = User.objects.get(username='admin')
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'User' is not defined
>>>
here is the model:
from django.db import models
from django.utils import timezone
from django.contrib.auth import get_user_model
User = get_user_model()
class Post(models.Model):
class Status(models.TextChoices):
DRAFT = 'DF', 'Draft'
PUBLISHED = 'PB', 'Published'
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250)
author = models.ForeignKey(
User, on_delete=models.CASCADE,related_name='blog_posts'
)
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=2,choices=Status.choices,default=Status.DRAFT)
class Meta:
ordering = ['-publish']
indexes = [models.Index(fields=['-publish']),]
def __str__(self):
return self.title
I tried to change this part of the code ' 'to this 'from django.contrib.auth.models import User' but it did not help
Try running this before running user = User.objects.get(username='admin')
:
>>> from django.contrib.auth.models import User
Hope this helps!