前提・実現したいこと
初めて質問させていただきます。python, Djangoの初心者です。
Groupモデルを作成して、Groupの中にあるグループ名をGroup.objects.filterで引っ張ってきたいのですが、
ローカル環境からアクセスしてもtype object 'Group' has no attribute 'objects'というエラーが発生します。
発生している問題・エラーメッセージ
下記はgoogle chromでlocalhost:8000でアクセスした時に表示されているエラーです
AttributeError at /sns/groups type object 'Group' has no attribute 'objects' Request Method: GET Request URL: http://localhost:8000/sns/groups Django Version: 2.2.1 Exception Type: AttributeError Exception Value: type object 'Group' has no attribute 'objects'
該当のソースコード
____________________________________________ #forms.py from django import forms from.models import Message,Group,Friend,Good from django.contrib.auth.models import User #Groupの選択メニューフォーム作成 class GroupSelectForm(forms.Form): def __init__(self, user, *args, **kwargs): super(GroupSelectForm, self).__init__(*args, **kwargs) self.fields["groups"] = forms.ChoiceField( choices = [("-", "-")] + [(item.title, item.title) for item in Group.objects.filter(owner = user)], ) __________________________________________ #models.py from django.db import models from django.contrib.auth.models import User #Groupクラス class Group(models.Model): owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="group_owner") title = models.CharField(max_length=100) def __str__(self): return self.title __________________________________________
試したこと
Groupモデルでモデルの追加(adminにアクセスして追加)や、周辺や関連コードの打ち間違えの確認などしましたが、変わらずエラーが出ております。
また調べていると、objectsはモデルクラスを生成すると自動的に用意してくれる属性だと書いてありました。
従って、Groupモデルにobjects属性が無い、ということはGroupがモデルとして認識されてないのでしょうか?もしくはobjectsを属性として認識させるためには何か別の作業が必要なのでしょうか?
補足情報(FW/ツールのバージョンなど)
python3.7

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/05 05:35
2019/06/05 05:42
2019/06/05 05:46