开发者

Django Select query with specific column

开发者 https://www.devze.com 2023-03-05 19:31 出处:网络
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 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开发者_Go百科 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 :(


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')
0

精彩评论

暂无评论...
验证码 换一张
取 消