Reload django object from database

Is it possible to refresh the state of a django object from database? I mean behavior roughly equivalent to: new_self = self.__class__.objects.get(pk=self.pk) for each field of the record: setattr(self, field, getattr(new_self, field)) UPDATE: Found a reopen/wontfix war in the tracker: http://code.djangoproject.com/ticket/901. Still don’t understand why the maintainers don’t like this. 4 Answers 4

How to limit the maximum value of a numeric field in a Django model?

Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField. Although the former can be restricted to the number of decimal places stored and the overall number of characters stored, is there any way to restrict it to storing only numbers within a certain range, e.g. 0.0-5.0 ? Failing that, is … Read more

In a django model custom save() method, how should you identify a new object?

I want to trigger a special action in the save() method of a Django model object when I’m saving a new record (not updating an existing record.) Is the check for (self.id != None) necessary and sufficient to guarantee the self record is new and not being updated? Any special cases this might overlook? 13 … Read more

Automatic creation date for Django model form objects?

What’s the best way to set a creation date for an object automatically, and also a field that will record when the object was last updated? models.py: created_at = models.DateTimeField(False, True, editable=False) updated_at = models.DateTimeField(True, True, editable=False) views.py: if request.method == ‘POST’: form = MyForm(request.POST) if form.is_valid(): obj = form.save(commit=False) obj.user = request.user obj.save() return … Read more

How to express a One-To-Many relationship in Django?

I’m defining my Django models right now and I realized that there wasn’t a OneToManyField in the model field types. I’m sure there’s a way to do this, so I’m not sure what I’m missing. I essentially have something like this: class Dude(models.Model): numbers = models.OneToManyField(‘PhoneNumber’) class PhoneNumber(models.Model): number = models.CharField() In this case, each … Read more