Search code examples
pythondjangoinline-formset

Django inlineformsets and general noobishness


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!


Solution

  • 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.

    title = models.CharField(max_length=1000)
    text = models.CharField(max_length=5000)
    

    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 existing StoryForm in the same view.

    So, the logic would be the following:

    author_form = AuthorForm(request.POST)
    if author_form.is_valid():
        author = author_form.save()
        story_form = StoryForm(request.POST)
        if story_form.is_valid():
            story = story_form.save(commit=False)
            story.author = author
            story.save()
    

    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.