| Issue 24: | Saving a model in newforms-admin fails because MPTT fields are Null | |
| 1 person starred this issue and may be notified of changes. | Back to list |
Here is the Model in question:
----
class Page(models.Model):
name = models.CharField(max_length=25)
title = models.CharField(max_length=80,null=True)
description = models.TextField(null=True)
menu_text = models.CharField(max_length=25, null=True, help_text="Leave
blank to exclude from menu.")
sequence = models.IntegerField(default=0)
page_url = models.CharField(max_length=20, unique=True)
template = models.ForeignKey(Template, limit_choices_to =
{'page_template': True})
content = models.TextField()
# The following fields are for implementing MPTT
parent = models.ForeignKey('self', null=True, blank=True,
related_name='children')
lft = models.PositiveIntegerField()
rght = models.PositiveIntegerField()
tree_id = models.PositiveIntegerField()
level = models.PositiveIntegerField()
def __unicode__(self):
return self.name
try:
mptt.register(Page)
except mptt.AlreadyRegistered:
pass
----
When attempting to add a record, newforms-admin reports "This field is
required" for lft, rght, tree_id, and left.
Oct 12, 2008
This is expected behaviour - if you only want form validation to pass (in the knowledge that the fields will actually be populated by MPTT before the model is saved), you just need to add blank=True to your fields. If you don't want these fields to be editable at all when you're specifying them explicitly, you could set editable=False, which is what MPTT does for the fields it generates: http://docs.djangoproject.com/en/dev/ref/models/fields/#editable
Status:
Invalid
|
I have determined one workaround: When explicitly adding the four fields to a model, specify null=True and blank=True: lft = models.PositiveIntegerField(null=True, blank=True) rght = models.PositiveIntegerField(null=True, blank=True) tree_id = models.PositiveIntegerField(null=True, blank=True) level = models.PositiveIntegerField(null=True, blank=True) The database will no longer reflect the model at this point, but form validation at least passes.