15π
Does insert_history use self? Or does it create a new History object?
If it creates a new object, Iβd do it like this:
class History(models.Model):
@classmethod
def insert_history(cls, field1, field2, field3):
# Here be code
To call it
from app.models import History
History.insert_history(field1, field2, field3)
BTW, the conventional name for a method creating new objects is create
. Also, have a look at https://docs.djangoproject.com/en/1.9/ref/models/instances/#creating-objects
4π
To insert data to the History model I will always have to use the
insert_history
function.Yes, it will set the field3, a datetime, based on some logic that I will write inside
insert_history
The easiest way is to override the save
method:
class History(models.Model):
field1 = models.ForeignKey(Request)
field2 = models.BooleanField()
field3 = models.DateTimeField()
def __unicode__(self):
return unicode(self.field1.id) # __unicode__ should return unicode,
# not string.
class Meta: #
ordering = ['-field3']
def save(self, *args, **kwargs):
self.field3 = your calculated value
super(History, self).save(*args, **kwargs)
Now, whenever you save your method β field3
βs value will be whatever is the result of the calculation in your custom save method. You donβt need to modify anything in your views for this to work.
- Getting error with is_popup variable in django 1.9
- Colorizing the output of Django tests
- Python Django custom template tags register.assignment_tag not working
- How do I serve media files in a local Django environment?
1π
I think that it is most appropriate to use custom manager https://docs.djangoproject.com/en/dev/topics/db/managers/ for this problems.
- How to set settings.LOGIN_URL to a view function name in Django 1.5+
- Django rest framework api_view vs normal view
- Django ChoiceField populated from database values
- Is there a Django template tag that lets me set a context variable?
- Django filter through multiple fields in a many-to-many intermediary table
1π
Just select your History instance (eg. with primary key 1):
hist = History.objects.get(pk=1)
β¦and call your method using the hist
variable:
hist.insert_history(...)
1π
Just to extend on answer by @burhankhalid, as I were unable to comment due to rather high-ish rep requirement, I had a similar need to alter another model while saving mine, but solution proposed by Burhan Khalid helped me to start.
The model I needed to modify, I had a reference-to from this one
My proposal assumes that Request would have a βattrib1β, and to that it tries to save the value from History.field2
class History(models.Model):
field1 = models.ForeignKey(Request)
field2 = models.BooleanField()
field3 = models.DateTimeField()
def __unicode__(self):
return unicode(self.field1.id) # __unicode__ should return unicode,
# not string.
class Meta: #
ordering = ['-field3']
def save(self, *args, **kwargs):
self.field3 = your calculated value
self.field1.attrib1 = self.field2 # for instance
self.field1.save() # don't forget to save the other
super(History, self).save(*args, **kwargs)
So the whole concept of rewriting the save() method and modifying (and saving) other objects as well made the difference
- Libmysqlclient.18.dylib image not found when using MySQL from Django on OS X
- How do you use Django-filter's '__in' lookup?
- Check for request.GET variable in the template