Django 选择特定列的查询
我有一个名为 MessagePerson
的模型,其中包含描述特定人员消息的详细信息字段,该人员的详细信息位于 Personal
模型中。
我什至有单独的模型,其中有对 Personal
类的外键引用。
class Personal(models.Model):
name = models.CharField(max_length=20,primary_key=True)
email = models.EmailField(blank=True,null=True)
address = models.CharField(max_length=50,blank=True,null=True)
contact = models.CharField(max_length=20)
pic = models.FileField(upload_to='image/',blank=True,null=True)
def __unicode__(self):
return self.name
class MessagePerson(models.Model):
person = models.ForeignKey(Personal, related_name='msg')
mname = models.CharField(max_length=30)
mPhone = models.CharField(max_length=20,blank=True,null=True)
memail = models.EmailField(blank=True,null=True)
message = models.CharField(max_length=200,blank=True,null=True)
def __unicode__(self):
return self.person.name
class GData(models.Model):
person = models.ForeignKey(Personal, related_name='everyperson')
place = models.CharField(max_length=40)
typeOfProperty = models.CharField(max_length=30)
typeOfPlace = models.CharField(max_length=20)
price = models.IntegerField()
def __unicode__(self):
return self.person.name
现在我需要的是,我有 GData
的 id
,我应该使用它为特定的“MessagePerson使用名称
个人”保存消息`
我使用的查询是: Views.py
def message(request, g_id):
n = request.POST['bname']
p = request.POST['bphone']
e = request.POST['bemail']
m = request.POST['bmsg']
mperson = GData.objects.get(id=g_id).values('person')
msg_job = MessagePerson(person=mperson, mname=n, mPhone=p, memail=e, message=m)
msg_job.save()
return render_to_response('ivent.html', context_instance=RequestContext(request))
现在的问题是,没有任何错误,它甚至没有存储消息:(
请帮助我获取结果:(
I have a model called MessagePerson
which has the fields of details which depict the messages for a particular person, who's details are in Personal
models.
I even have separate model which has a foreign key reference to Personal
class.
class Personal(models.Model):
name = models.CharField(max_length=20,primary_key=True)
email = models.EmailField(blank=True,null=True)
address = models.CharField(max_length=50,blank=True,null=True)
contact = models.CharField(max_length=20)
pic = models.FileField(upload_to='image/',blank=True,null=True)
def __unicode__(self):
return self.name
class MessagePerson(models.Model):
person = models.ForeignKey(Personal, related_name='msg')
mname = models.CharField(max_length=30)
mPhone = models.CharField(max_length=20,blank=True,null=True)
memail = models.EmailField(blank=True,null=True)
message = models.CharField(max_length=200,blank=True,null=True)
def __unicode__(self):
return self.person.name
class GData(models.Model):
person = models.ForeignKey(Personal, related_name='everyperson')
place = models.CharField(max_length=40)
typeOfProperty = models.CharField(max_length=30)
typeOfPlace = models.CharField(max_length=20)
price = models.IntegerField()
def __unicode__(self):
return self.person.name
Now the thing I require is, I have the id
of GData
using which i should save a message for the particular 'MessagePersonusing name of
Personal`
The query I used is :
Views.py
def message(request, g_id):
n = request.POST['bname']
p = request.POST['bphone']
e = request.POST['bemail']
m = request.POST['bmsg']
mperson = GData.objects.get(id=g_id).values('person')
msg_job = MessagePerson(person=mperson, mname=n, mPhone=p, memail=e, message=m)
msg_job.save()
return render_to_response('ivent.html', context_instance=RequestContext(request))
Now the thing is, without any error, it is not even storing the message :(
Please help me to get the result :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
突出的是:
可能应该是:
您还应该重新考虑您的视图逻辑,只是快速查看这样的东西稍微好一点,但可能可以改进:
Thing that stands out is that:
should probably be:
You should also probably rethink your view logic, just looking quickly something like this is slightly better, but can probably be improved: