I have the following Abstract:
class A:
clientname ...
logs = models.ManyToManyField(B, blank=True, null=True)
class B.
message ...
timestamp = models.DateTimeField()
def save(self, *args, **kwargs):
self.timestamp = datetime.now()
super(B, self).save(*args, **kwargs)
Now, if a message is saved I want it to have a timestamp, always. And I do not want to change the many2many relationship. Is this possible? And if It is how would I go about writing it?
UPDATE:
my view
def log(request):
if request.method == 'POST':
log, created = B.objects.get_or_create(message=request.POST['message'])
client = \
A.objects.get(clientname=request.POST['clientname'])
client.logs.add(log)
return HttpResponse(content="OK", mimetype="text/plain", status=200)
else:
return HttpResponse(content="Failed", mimetype="text/plain", status=400)
RESOLUTON:
I did it, this is kind of a workaround:
def log(request):
if request.method == 'POST':
time = datetime.now()
log, created = \
Log.objects.get_or_create(message=request.POST['message'], \
timestamp=time)
client = \
Thinclient.objects.get(hostname=request.POST['clientname'])
client.logs.add(log)
return HttpResponse(content="OK", mimetype="text/plain", status=200)
else:
return HttpResponse(content="Failed", mimetype="text/plain", status=400)
UPDATE:
No actually that wasn’t what I wanted either because this will create new message instances even if the message is the same
add save() to the class B