何かが足りないようです。ユーザーが自分の執筆用のジャンルを選択できるようにしようとしています。これは、作業(執筆)テーブル/モデルからジャンルテーブル/モデルへの外部キーとして設定されます。
エラー:Django AttributeError: 'str' object has no attribute '_default_manager'
これが私が今やっている方法です。
Form.py
class post_ow(forms.Form):
def __init__(self, *args, **kwargs):
super(post_ow, self).__init__(*args, **kwargs)
self.fields['one_shot'].label = "Single Chapter or Multiple Chapters"
c = []
genre = Genre.objects.filter(poem = False).order_by('name')
for b in genre:
c.append((b.id,b.name))
self.fields['genre'] = forms.ChoiceField(choices = c)
title = forms.CharField(error_messages = {'required' : 'Title Required'}, widget= forms.TextInput(attrs={'style':'width=600px'}))
summery = forms.CharField(widget = forms.Textarea(), error_messages= {'required' : 'Summery Required'})
genre = forms.ChoiceField(choices = ())
one_shot = forms.ChoiceField(choices = (
('one', 'Single Chapter'),
('multi', 'Multiple Chapters'),
))
Model.py
class Genre(models.Model):
name = models.CharField(max_length=20)
poem = models.BooleanField()
ordernumber = models.IntegerField()
def __unicode__(self):
return self.name
class Work(models.Model):
title = models.CharField(max_length=30)
summery = models.TextField()
user = models.ForeignKey(User)
date_published = models.DateField()
date_updated = models.DateField()
one_shot = models.BooleanField()
completed = models.BooleanField()
genre = models.ForeignKey(Genre)
TYPES = (
('FF', 'Fan Fiction'),
('OF', 'Original Work'),
('PW', 'Poetry Work'),
)
types = models.CharField(max_length=2, choices=TYPES)
fandom = models.ForeignKey(Book, blank = True, null= True)
def __unicode__(self):
return self.title + '-' + self.user.email
view.pyでエラーを引き起こす行:
g = get_object_or_404('Genre', id = form.cleaned_data['genre'])
pw = Work(title = form.cleaned_data['title'], summery = form.cleaned_data['summery'], user = request.user, one_shot = True, types = 'pw',
date_published = current_date, date_updated = current_date, completed = True, genre = g)