Skip to content

Instantly share code, notes, and snippets.

@narkeeso
Created May 6, 2012 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save narkeeso/3dbbf80d1eecd33a726e to your computer and use it in GitHub Desktop.
Save narkeeso/3dbbf80d1eecd33a726e to your computer and use it in GitHub Desktop.
class BandGenreForm(ModelForm):
class Meta:
model = BandGenre
fields = ('genre',)
class Genre(models.Model):
name = models.CharField(max_length=765)
class Meta:
db_table = u'genre'
def __unicode__(self):
return u'%s' % (self.name)
class BandGenre(models.Model):
band = models.ForeignKey(Band, unique=True)
genre = models.ManyToManyField(Genre)
created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
edited = models.DateTimeField(auto_now=True, null=True, blank=True)
class Meta:
db_table = u'band_genre'
def __unicode__(self):
return u'%s <-> %s' % (self.band, self.genre)
def EditBandDetail(request, band_id, template='band_detail.html'):
try:
band = get_object_or_404(Band, pk=band_id)
genre = get_object_or_404(BandGenre, pk=band_id)
except Band.DoesNotExist:
raise Http404(u'Not found')
genre_form = BandGenreForm()
if request.POST:
genre_form = BandGenreForm(request.POST, initial=genre)
if genre_form.is_valid():
genre_form.save()
return HttpResponseRedirect('/lookup/')
else:
genre_form = BandGenreForm()
context = Context({
'band': band,
'genre': genre,
'genre_form': genre_form,
})
return render(request, template, context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment