Django 选择特定列的查询

发布于 2024-11-06 23:49:17 字数 1896 浏览 1 评论 0原文

我有一个名为 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

现在我需要的是,我有 GDataid,我应该使用它为特定的“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 ofPersonal`

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

压抑⊿情绪 2024-11-13 23:49:17

突出的是:

mperson = GData.objects.get(id=g_id).values('person')

可能应该是:

mperson = GData.objects.get(id=g_id).person

您还应该重新考虑您的视图逻辑,只是快速查看这样的东西稍微好一点,但可能可以改进:

from django.shortcuts import get_object_or_404 

def message(request, g_id):
    mperson = get_objects_or_404(GData, id=g_id).person

    if request.method == 'POST':
        n = request.POST.get('bname')
        p = request.POST.get('bphone')
        e = request.POST.get('bemail')
        m = request.POST.get('bmsg')
        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))

    return HttpResponse('Not a post request')

Thing that stands out is that:

mperson = GData.objects.get(id=g_id).values('person')

should probably be:

mperson = GData.objects.get(id=g_id).person

You should also probably rethink your view logic, just looking quickly something like this is slightly better, but can probably be improved:

from django.shortcuts import get_object_or_404 

def message(request, g_id):
    mperson = get_objects_or_404(GData, id=g_id).person

    if request.method == 'POST':
        n = request.POST.get('bname')
        p = request.POST.get('bphone')
        e = request.POST.get('bemail')
        m = request.POST.get('bmsg')
        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))

    return HttpResponse('Not a post request')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文