I'm writing a webblog app in Django. I currently have 2 models Post and PostMeta. Post is a standard post style model with fields such as author, title, content etc. It also contains a single many-to-many field called post_meta which is associated with my second model PostMeta. PostMeta is a simple name/value model with two fields, meta_key and meta_value.
What I'm trying to do is customize the Post model's form in the admin interface to be more intuitive. Specifically I want to abstract the creation of PostMeta associations rather than see the unintuitive select box which is redered by default for the admin. I want to instead show a text field in place of this select box where the user can enter a comma separated list of tags associated with the post. When the form is submitted I want to split the tag field's input into individual tags and save each as a PostMeta where meta_key will be set to "TAG" and meta_value will be one of the comma separated strings.
The problem I am having is I can't seem to get it to save correctly. I'm not sure if there is a problem with my syntax (I'm relativly new to python) or if there is somthing else I need to do which I may have missed. Here is a snippet of my admin.py:
class PostAdminForm(forms.ModelForm):
tags = forms.CharField(max_length=200)
class Meta:
model = Post
def save(self, commit=True):
model = super(PostAdminForm, self).save(commit=False)
if commit:
model.save()
splitTags = self.cleaned_data['tags'].split(',')
for tag in splitTags:
pm = PostMeta(meta_key="TAG", meta_value=tag)
pm.save()
model.post_meta.add(pm)
return model
class PostAdmin(admin.ModelAdmin):
model = Post
form = PostAdminForm
admin.site.register(Post, PostAdmin)
Any advice or suggestions on how to make this work would be great. Still learning :\