2011-12-07 205 views
4

我有一個模型,看起來像這樣:Django的多個模型,一種形式

class Movie(models.Model): 
    title = models.CharField(max_length=200) 
    slug = models.SlugField(max_length=200) 
    user = models.ForeignKey(User) 
    created_on = models.DateTimeField(default=datetime.datetime.now()) 

    class Meta: 
     ordering = ['-title'] 

    def __unicode__(self): 
     return self.title 

class MovieScreener(models.Model): 
    screener_asset = models.FileField(upload_to='movies/screeners/') 
    movie = models.ForeignKey(Movie) 

class MovieTrailer(models.Model): 
    trailer_asset = models.FileField(upload_to='movies/trailers/', blank=True, null=True) 
    description = models.TextField() 
    movie = models.ForeignKey(Movie) 

class MoviePoster(models.Model): 
    poster_asset = models.FileField(upload_to='movies/posters/', blank=True, null=True) 
    movie = models.ForeignKey(Movie) 

而且我的形式是這樣的:

class MovieForm(forms.ModelForm): 
    class Meta: 
     model = Movie 
     exclude = ('user','created_on') 

class MovieScreenerForm(forms.ModelForm): 
    class Meta: 
     model = MovieScreener 
     exclude = ('movie',) 

class MovieTrailerForm(forms.ModelForm): 
    class Meta: 
     model = MovieTrailer 
     exclude = ('movie',) 

class MoviePosterForm(forms.ModelForm): 
    class Meta: 
     model = MoviePoster 
     exclude = ('movie',) 

這裏是我的views.py(這是哪裏它看起來醜陋)

@login_required 
def create_movie(request, template_name="explore/create_movie.html"): 
    if request.method == 'POST': 
     movie_form = MovieForm(data=request.POST) 
     movie_screener_form = MovieScreenerForm(data=request.POST, files=request.FILES, prefix="a") 
     movie_trailer_form = MovieTrailerForm(data=request.POST, files=request.FILES, prefix="b") 
     movie_poster_form = MoviePosterForm(data=request.POST, files=request.FILES, prefix="c") 

     if movie_form.is_valid() and movie_screener_form.is_valid() and movie_trailer_form.is_valid(): 
      movie_form.instance.user = request.user 
      movie = movie_form.save() 

      movie_screener_form.save(commit=False) 
      movie_screener_form.instance.movie = movie 
      movie_screener_form.save() 

      movie_trailer_form.save(commit=False) 
      movie_trailer_form.instance.movie = movie 
      movie_trailer_form.save() 

      movie_poster_form.save(commit=False) 
      movie_poster_form.instance.movie = movie 
      movie_poster_form.save() 

      url = urlresolvers.reverse('explore') 
      return redirect(url) 
    else: 
     movie_form = MovieForm(instance=request.user, label_suffix='') 
     movie_screener_form = MovieScreenerForm(prefix="a", label_suffix='') 
     movie_trailer_form = MovieTrailerForm(prefix="b", label_suffix='') 
     movie_poster_form = MoviePosterForm(prefix="c", label_suffix='') 

context = RequestContext(request, locals()) 
return render_to_response(template_name, context) 

我views.py似乎很重複,這是做的正確方法還是有更好的方式來做到這一點?

感謝

Ĵ

回答

3

真的不能想辦法在定義模型或形式的條款,但你可以削減一些線路與以下。

mfs = [movie_screener_form, movie_trailer_form, movie_poster_form] 

for mf in mfs: 
    mf.save(commit=False) 
    mf.instance.movie = movie 
    mf.save() 
3

有一兩件事你可以做的是繼續前進需要它從視圖的形式本身在初始化時,將它作爲一個參數模型形式的電影實例的設置形成。下面是一個實現的例子,但是這可能會成爲其他人可以繼承的基本表單類,從而使您無需單獨進行重寫。這段代碼沒有經過測試,我只是想大聲...

class MovieScreenerForm(forms.ModelForm): 
    class Meta: 
     model = MovieScreener 
     exclude = ('movie',) 

    def __init__(self, movie, *args, **kwargs): 
     super(MovieScreen, self).__init__(*args, **kwargs) 
     self.movie = movie 

    def save(self, commit=True): 
     instance = super(MovieScreenerForm, self).save(commit=False) 
     instance.move = self.movie 
     instance.save() 
0

如果我理解正確的設計,則:

  • 每部影片都有一個篩選
  • 電影永遠不會有一個以上的篩選
  • 電影可能有一個拖車
  • 電影從來沒有超過一個拖車
  • 電影可能有一個海報
  • 電影從來沒有超過一個海報

這是正確的嗎?

如果我的假設是正確的,那麼你可以在電影模型中擁有所有的字段。 (預告片和海報已經可以空,所以它們是可選的)。所以你只需要一個模型和一個表單。