Python Django'function'对象没有属性'objects'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15450060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:08:48  来源:igfitidea点击:

Django 'function' object has no attribute 'objects'

pythondjango

提问by donkeyboy72

My apps allow you to like a picture then it redirects you to the same page

我的应用程序允许您喜欢图片,然后它会将您重定向到同一页面

I get then error when I try to like a picture I can create a like objects with the shell prompt but why I get this error? thank for helping me

当我尝试喜欢一张图片时出现错误 我可以使用 shell 提示创建一个喜欢的对象,但为什么会出现此错误?谢谢你帮助我

AttributeError at /like/3/
function' object has no attribute 'objects'Request Method: GET 
Request URL: http://127.0.0.1:8000/like/3/ 

Exception Value: 'function' object has no attribute 'objects' 
Traceback:
File "C:\Python26\Lib\site-packages\django\core\handlers\base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\o\mysite\pet\views.py" in Like
195.     new_like, created = Like.objects.get_or_create(user=request.user, picture_id=picture_id)

This is parts of my views.py

这是我的 views.py 的一部分

def Like(request,picture_id):
    pid = picture_id
    new_like, created = Like.objects.get_or_create(user=request.user, picture_id=picture_id)
    p = Picture.objects.get(pk=pid)
    if created:
        HttpResponseRedirect(reverse('world:url_name'))
    else:
        HttpResponseRedirect(reverse('world:url_name'))

My URLconf:

我的 URLconf:

     url(

Parts of my model: r'^like/(?P\d+)/$', 'pet.views.Like', name = 'Like' ), My boat.html

我的模型部分:r'^like/(?P\d+)/$', 'pet.views.Like', name = 'Like' ), Myboat.html

 {% if picture %}
 <ul>
    {% for pet in picture %}
    <li><b>description</b> = {{ pet.description}}<br/>
        {% if pet.image %}
 <li>
    <a href ="{% url world:Like pet.id %}">
        <img src= "{{ pet.image.url }}" style="cursor:pointer">
    </a>
 <li>
        {% endif %}
 {% endfor %}
 </ul>
 {% endif %}
 <a href="{% url world:PictureCreator %}">Add Pictures to your board</a><br/>

My models.py

我的模型.py

class Picture(models.Model):
    user = models.ForeignKey(User)
    board = models.ForeignKey(Board,blank=False,null=False)
    image = models.FileField(upload_to="images/",blank=True)
    description = models.TextField()
    is_primary = models.BooleanField(default=False)
    def __unicode__(self):
        return self.description

class Like(models.Model):
    user = models.ForeignKey(User)
    picture = models.ForeignKey(Picture)
    created = models.DateTimeField(auto_now_add=True)    

采纳答案by dm03514

your view function name is defined as Likeand your model is named Like

您的视图函数名称被定义为Like,您的模型被命名为Like

you define Likeas a function so when you go to access Like.objectspython does not see your model Likebut the function Like

你定义Like为一个函数,所以当你去访问Like.objectspython 时看不到你的模型Like而是函数Like

you could rename your view function

你可以重命名你的视图函数

url(r'^like/(?P\d+)/$', 'pet.views.change_name_no_conflict', name = 'Like' )


def change_name_no_conflict(request,picture_id):
  pass

回答by Sandeep G

Model name and view name shouldn't be same.

模型名称和视图名称不应相同。

回答by DevB2F

I got this same error when doing

我在做时遇到了同样的错误

 from django.contrib.auth import get_user_model
 User = get_user_model

adding () solved the error:

添加 () 解决了错误:

 from django.contrib.auth import get_user_model
 User = get_user_model()

回答by Ankit Anand

function name and model name does depend on name, function name should be same as url name we define url in urls.py model name depend on function data member, its means as for example when we take data from user and save in database then we call that object from its model name ex= u_data = registration() ,this is used for user data seve and define that fun where we send data to save means target url and in its related view.

函数名和模型名确实依赖于名称,函数名应该和我们在 urls.py 中定义的 url 的 url 名相同 模型名依赖于函数数据成员,例如当我们从用户那里获取数据并保存在数据库中时,我们从其模型名称 ex= u_data = registration() 调用该对象,这用于用户数据服务并定义我们将数据发送到保存的乐趣意味着目标 url 及其相关视图。

回答by Aman Jain

Define object:

定义对象:

objects = models.Manager()

Your error will solve!

你的错误会解决!