Django admin form error, never saves foreign key even if present -
i know title confusing let me explain. implementing small wiki apphook django + django-cms project. trying add sections wiki, wiki pages can live specific sections allowing bit of structure.
here model.py
:
from django.db import models djangocms_text_ckeditor.fields import htmlfield class wikipage(models.model): slug = models.slugfield(max_length=50,primary_key=true) name = models.charfield(max_length=50) content = htmlfield(blank=true) section = models.foreignkey('wikisection', related_name='pages', db_index=true) def __str__(self): return self.name class wikisection(models.model): slug = models.slugfield(max_length=50, primary_key=true) name = models.charfield(max_length=50) def __str__(self): return self.name
here admin.py
:
from django.contrib import admin .models import wikipage, wikisection django import forms forms import wikipageform, wikisectionform class wikipageadmin(admin.modeladmin): form = wikipageform admin.site.register(wikipage, wikipageadmin) class wikisectionadmin(admin.modeladmin): form = wikisectionform admin.site.register(wikisection, wikisectionadmin)
ignore horrible import, know.
here forms.py
:
from django import forms .models import wikipage, wikisection class wikipageform(forms.modelform): class meta: model = wikipage exclude = ['slug'] def clean(self): cleaned_data = super(wikipageform, self).clean() print cleaned_data class wikisectionform(forms.modelform): class meta: model = wikisection exclude = ['slug']
now when try , fill in field , save admin form, tells me section field required, though fill in field. realised field not present in clean() method. prints:
{'content': u'<p>sdfsdf</p>\r\n', 'name': u'fdfsdf'}
edit: checked , data isn't sent in post
wow, such stupid mistake on part. since excluding slug field both models, needed override save method , provide slug object. here revised models.py
:
from django.db import models django.utils.text import slugify djangocms_text_ckeditor.fields import htmlfield class wikipage(models.model): slug = models.slugfield(max_length=50,primary_key=true) name = models.charfield(max_length=50) content = htmlfield(blank=true) section = models.foreignkey('wikisection', related_name='pages', db_index=true) def __str__(self): return self.name <!-- added --> def save(self, *args, **kwargs): self.slug = slugify(self.name) super(wikipage, self).save(*args, **kwargs) <!-- added --> class wikisection(models.model): slug = models.slugfield(max_length=50, primary_key=true) name = models.charfield(max_length=50) def __str__(self): return self.name <!-- , --> def save(self, *args, **kwargs): self.slug = slugify(self.name) super(wikisection, self).save(*args, **kwargs) <!-- , -->
Comments
Post a Comment