Putting a message in an inbox (Django models) -


i have couple of models , little confused how create of associations. have profiles, events, messages, , inboxes. each profile has eventlist holds events. each message associated event too. each inbox associated profile , multiple messages. want is, whenever message object created, inserted inbox of every user holds event message associated in eventlist. providing models , view i'm writing:

class profile(models.model):     user = models.onetoonefield(user)     name = models.charfield(max_length=50)     eventlist = models.manytomanyfield(event, blank="true", null="true", related_name='event_set+')     ownedevent = models.manytomanyfield(event, blank="true", null="true", related_name='owned_set')     def __unicode__(self):             return self.name  class inbox(models.model):     def __unicode__(self):         return self.user.name     user = models.onetoonefield(profile)     message = models.manytomanyfield(message, blank="true", null="true")     read = models.booleanfield(default = 0)  class message(models.model):     def __unicode__(self):         return unicode(self.body)     def save(self, *args, **kwargs):         if not self.id:             self.created = datetime.datetime.today()         super(message, self).save(*args, **kwargs)     body = models.charfield(max_length=250)     eid = models.foreignkey(event)   @login_required def sendmail(request):      event_id = request.post['event_id']     e = event.objects.get(id = event_id)     text = request.post['body']      m = message(eid = e, body = text)     m.save()      users = e.eventlist_set.all()      return httpresponse(status = 200) 

if want happen can put relevant code in message.save() method or post-save signal handler. if it's view can put code there. in either case, should work:

# msg message instance box in inbox.objects.filter(user__eventlist=msg.eid):     box.message.add(msg) 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -