Django 内联表单集和一般新手
我很抱歉在一天内问了两个关于类似主题的问题,但我对 django 的新手已经完全占据了。基本上我有两个模型:
class Story(models.Model):
title = models.CharField(max_length=1000)
text = models.CharField(max_length=5000)
date = models.DateField(default=datetime.date.today, editable=False)
likes = models.IntegerField(default='0')
dislikes = models.IntegerField(default='0')
views = models.IntegerField(default='0')
author = models.ForeignKey('Author', related_name="username")
class Author(models.Model):
username = models.CharField(max_length=120, unique=True)
email = models.EmailField(blank=True)
firstname = models.CharField(max_length=500, blank=True)
lastname = models.CharField(max_length=500, blank=True)
正如你可以的,这个想法是有人提交一个故事,他们的名字进入作者的表。很简单的东西。我的问题是创建一个表单,允许用户提交他们的故事和名字。我尝试过 inlineformset,但没有运气,我只是找不到一种方法来执行我所知道的幕后简单的 AND sql 语句。
这是我的表单模型: StoryAuthorFormSet = inlineformset_factory(Author, Story)
class StoryForm(ModelForm):
class Meta:
model = Story
exclude = ('date', 'views', 'likes', 'dislikes', 'author')
这是我的观点
def submit_story(request):
if request.method == 'POST':
form = StoryForm(request.POST)
if form.is_valid:
#watch this nest part be aware of the author bit
author = form.save(commit=False)
story = StoryAuthorFormSet(request.POST, instance=author)
if author_formset.is_valid():
story.save()
author_formset.save()
return HttpResponseRedirect('/thanks/')
else:
form = StoryForm()
author_formset = StoryAuthorFormSet(instance=Author())
return render_to_response('submit/submit_story.html', locals(), context_instance=RequestContext(request))
,我意识到他们可能是一个我错过的简单解决方案......但我很困惑任何想法都会非常感激......如此之多以至于我将为任何有需要的人提供一些免费的 Clojure 和 Erlang 编程!
I'm sorry to ask two questions about similar topics in one day but my noobishness with django has just completely taken over. Basically I have two models:
class Story(models.Model):
title = models.CharField(max_length=1000)
text = models.CharField(max_length=5000)
date = models.DateField(default=datetime.date.today, editable=False)
likes = models.IntegerField(default='0')
dislikes = models.IntegerField(default='0')
views = models.IntegerField(default='0')
author = models.ForeignKey('Author', related_name="username")
class Author(models.Model):
username = models.CharField(max_length=120, unique=True)
email = models.EmailField(blank=True)
firstname = models.CharField(max_length=500, blank=True)
lastname = models.CharField(max_length=500, blank=True)
as you can the idea is that someone submits a story and their name goes into the author's table. Pretty simple stuff. My problem is creating a form that will allow a user to submit their story and their name. I've tried an inlineformset and had no luck I just can't find a way to perform what I know is just a simple AND sql statement behind the scenes.
Here are my form models:
StoryAuthorFormSet = inlineformset_factory(Author, Story)
class StoryForm(ModelForm):
class Meta:
model = Story
exclude = ('date', 'views', 'likes', 'dislikes', 'author')
and here is my view
def submit_story(request):
if request.method == 'POST':
form = StoryForm(request.POST)
if form.is_valid:
#watch this nest part be aware of the author bit
author = form.save(commit=False)
story = StoryAuthorFormSet(request.POST, instance=author)
if author_formset.is_valid():
story.save()
author_formset.save()
return HttpResponseRedirect('/thanks/')
else:
form = StoryForm()
author_formset = StoryAuthorFormSet(instance=Author())
return render_to_response('submit/submit_story.html', locals(), context_instance=RequestContext(request))
I realise their is probably a simple solution that I've missed...but I am stumped any ideas would be greatly greatly appreciated.....so much so that I'll do some free Clojure and Erlang programming for anybody who needs it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的示例中不需要内联表单集。仅当我需要一次为给定作者提交多个故事时,我才会使用它。
我还想对你们的模型进行一些更改。
TextField 可能更合适,至少对于文本部分而言。
我会这样解决:
创建一个
AuthorForm
并在同一视图中使用它和现有的StoryForm
。因此,逻辑如下:
我们使用
instance
参数为表单指定一个已经存在的模型。例如,您想要更新现有的数据库条目。An inline formset is not required in your example. I'd use that only if I needed to submit several stories at once for a given author.
Also there are things that I'd change about your models.
A TextField might be more appropriate, at least for the text part.
I would resolve it like this:
create an
AuthorForm
and use both that and your existingStoryForm
in the same view.So, the logic would be the following:
The
instance
parameter let's use specify an already existing model for the form. E.g you want to update a already existing database entry.