我正在建立一个博客,我想统计访问我的页面并显示用户数量的所有用户
访问该页面的用户。并以填充的方式显示内容。
我正在使用GenericForeignKey
访问我的hitcount
包上的模型
基于hitcount
包的文档。在我完成创建模型并尝试运行服务器后,我发现以下错误
File "F:\professional blog in django\BLOG\blog\blogapp\models.py", line 48, in Post site_visitor = GenericForeignKey( TypeError: init() got an unexpected keyword argument 'object_id_fields'
有什么事情我做得不对吗 请帮忙
model.py
from django.db import models from django.contrib.auth.models import
User from datetime import datetime, date from django.db.models.fields
import related from django.urls import reverse from
django.contrib.contenttypes.fields import GenericForeignKey from
hitcount.models import HitCountMixin, HitCount
# Create your models here. from django.core.exceptions import ValidationError
class Post(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(
User, on_delete=models.CASCADE, null=True, blank=True)
body = models.TextField()
category = models.ForeignKey(
Category, on_delete=models.CASCADE, default=1)
date = models.DateField(auto_now_add=True)
image = models.ImageField(upload_to="image", validators=[image_vailid])
likes = models.ManyToManyField(User, related_name="like_post")
site_visitor = GenericForeignKey(
HitCountMixin, object_id_fields="object_pk", content_type_field="content_type")
def numbers_of_likes(self):
return self.likes.count()
def __str__(self):
return self.title + '| ' + str(self.author)
def get_absolute_url(self):
return reverse("home")
Error:
File "F:\professional blog in django\BLOG\blog\blogapp\models.py",
line 48, in Post
site_visitor = GenericForeignKey( TypeError: __init__() got an unexpected keyword argument 'object_id_fields'
问题正是它所说的:您试图创建一个带有关键字参数的类,该类不接受(source):
正如您所看到的,
GenericForeignKey
类只接受3个参数(类除外):ct_field
、fk_field
和for_concrete_model
。所以只有这三个可以用作关键字参数您需要传递不带关键字的参数,或者确保传递此参数的位置正确。从django文档和API reference开始
如果您不理解关键字参数和位置参数之间的区别,请转到python文档页面:arguments
编辑: 这是代码中错误的部分:
请阅读我为您提供的链接
相关问题 更多 >
编程相关推荐