I am working on creating a blog app in Django and while I was making the Query set in cmd I got the following error,
CODE IN CMD:
from django.contrib.auth.models import User >>> from blog.models import Post >>> user = User.objects.get(username='mratyunjay') >>> post = Post(title='Another post', ... slug='another-post', ... body='Post body.', ... author=user)
ERROR :
Traceback (most recent call last):
File "<console>", line 4, in <module>
File "C:UsersComputerDesktopprojectmy_envlibsite-packagesdjangodbmodelsbase.py", line 501, in __init__
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
TypeError: Post() got an unexpected keyword argument 'body'
admin.py :-
from django.contrib import admin
# Register your models here.
from .models import Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ( 'title', 'slug', 'author', 'publish', 'status')
list_filter = ( 'status', 'created', 'publish', 'author')
search_fields = ( 'title', 'body' )
prepopulated_fields = {'slug': ('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ( 'status', 'publish' )
models.py:-
from django.db import models
# Create your models here.
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish')
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=10, choices=STATUS_CHOICES, default='draft')
class Meta:
ordering = ( '-publish', )
def __str__(self):
return self.title
ERROR in models.py :-
User model imported from django.contrib.auth.models pylint(imported-auth-user) [5,1]
I am new to Django and don’t know how to resolve the user model, and while creating the query set another error came up. How can I resolve this ?
Advertisement
Answer
You are missing () with body field in your models.py file. It should look like this.
body = models.TextField()
After that, run the following commands,
python manage.py makemigrations python manage.py migrate
Then try writing your code in commandline