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:
JavaScript
x
8
1
from django.contrib.auth.models import User
2
>>> from blog.models import Post
3
>>> user = User.objects.get(username='mratyunjay')
4
>>> post = Post(title='Another post',
5
slug='another-post',
6
body='Post body.',
7
author=user)
8
ERROR :
JavaScript
1
6
1
Traceback (most recent call last):
2
File "<console>", line 4, in <module>
3
File "C:UsersComputerDesktopprojectmy_envlibsite-packagesdjangodbmodelsbase.py", line 501, in __init__
4
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
5
TypeError: Post() got an unexpected keyword argument 'body'
6
admin.py :-
JavaScript
1
14
14
1
from django.contrib import admin
2
3
# Register your models here.
4
from .models import Post
5
@admin.register(Post)
6
class PostAdmin(admin.ModelAdmin):
7
list_display = ( 'title', 'slug', 'author', 'publish', 'status')
8
list_filter = ( 'status', 'created', 'publish', 'author')
9
search_fields = ( 'title', 'body' )
10
prepopulated_fields = {'slug': ('title',)}
11
raw_id_fields = ('author',)
12
date_hierarchy = 'publish'
13
ordering = ( 'status', 'publish' )
14
models.py:-
JavaScript
1
26
26
1
from django.db import models
2
3
# Create your models here.
4
from django.utils import timezone
5
from django.contrib.auth.models import User
6
7
class Post(models.Model):
8
STATUS_CHOICES = (
9
('draft', 'Draft'),
10
('published', 'Published'),
11
)
12
title = models.CharField(max_length=250)
13
slug = models.SlugField(max_length=250, unique_for_date='publish')
14
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
15
body = models.TextField
16
publish = models.DateTimeField(default=timezone.now)
17
created = models.DateTimeField(auto_now_add=True)
18
updated = models.DateTimeField(auto_now=True)
19
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
20
21
class Meta:
22
ordering = ( '-publish', )
23
24
def __str__(self):
25
return self.title
26
ERROR in models.py :-
JavaScript
1
2
1
User model imported from django.contrib.auth.models pylint(imported-auth-user) [5,1]
2
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.
JavaScript
1
2
1
body = models.TextField()
2
After that, run the following commands,
JavaScript
1
4
1
python manage.py makemigrations
2
3
python manage.py migrate
4
Then try writing your code in commandline