The ticket open has a patch to it, but I found a simpler solution, although very ugly, which is to override the model's save method:
def save(self):
for field in self._meta.fields:
# Add in the default values
if not getattr(self, field.name):
setattr(self, field.name, field.get_default())
super(User, self).save()
I really don't like this solution, but it works, and does not require any changes/patches to the django libraries.
Cheers,
-j
On 6/21/06, Jamie Scheinblum <[EMAIL PROTECTED]> wrote:
Cheers,
-j
On 6/21/06, Jamie Scheinblum <[EMAIL PROTECTED]> wrote:
Hi,
I'm having trouble with default values in my model. I've tried to establish what the default for a column should be when the input doesn't supply a value. The model defaults seem to work fine when using django's python shell, but when I use either the admin interface or my own view, the defaults never make it into the database. Is this a sqlite3 limitation? Do the defaults not get carried over when using AddManipulator?
Thanks in advance!
-j
Here is my model:
class User(models.Model):
email_address = models.EmailField()
password = PasswordField(maxlength=8)
nickname = models.CharField(maxlength=20)
verified = models.BooleanField("user verified", default=False)
tz = models.IntegerField("timezone", default=8,
blank=True,
null=True)
type = models.CharField(maxlength=1, default=TYPE_REGULAR,
choices=USER_TYPES,
blank=True,
null=True)
class Admin:
pass
def __str__(self):
return self.email_address
and the view that I'm using
def signup(request):
manipulator = User.AddManipulator()
if request.POST:
# If data was POSTed, we're trying to create a new object.
new_data = request.POST.copy()
# Check for errors.
errors = manipulator.get_validation_errors(new_data)
if not errors:
# No errors. This means we can save the data!
manipulator.do_html2python(new_data)
new_object = manipulator.save(new_data)
# Redirect to the object's "edit" page. Always use a redirect
# after POST data, so that reloads don't accidently create
# duplicate entires, and so users don't see the confusing
# "Repost POST data?" alert box in their browsers.
return HttpResponseRedirect("/people/sign-in/welcome")
else:
# No POST, so we want a brand new form without any data or errors.
errors = new_data = {}
# Create the FormWrapper, template, context, response.
form = forms.FormWrapper (manipulator, new_data, errors)
return render_to_response(request, 'people/signup.html', {'form': form})
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---