如何将一个模型对象映射到视图中的另一个模型对象 [英] How do I map one models objects to another models objects in a view

查看:202
本文介绍了如何将一个模型对象映射到视图中的另一个模型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆信息记录,我想分配给不同的任务栏。

 #models.py 

class TaskBox(models.Model):
name = models.CharField(max_length = 64,blank = False)

def __str __(self):
返回u'%s'%(self.name)

class Admin:
pass

class InboxEntry(models.Model):
job_number = models.CharField(max_length = 14,unique = False,blank = False,null = False)
job_name = models.CharField(max_length = 64,unique = False,blank = False,null = False)
request = models.CharField(max_length = 64,choices = PRINT_CHOICES,blank = True,null = True)
date_due = models.DateTimeField((Due),auto_now = False)
note = models。 TextField(max_length = 1000,unique = False,blank = True,null = True)
assigned_by = models.ForeignKey(UserProfile,blank = False,null = False)
box = models.ForeignKey(TaskBox)
assigned_to = models.ManyToManyField(UserP rofile,related_name ='name',blank = True)
status = models.CharField(max_length = 30,choices = STATUS_CHOICES,default =等待操作)

def __str __(self) :
return u'%s%s'%(self.job_number,self.job_name)

class Admin:
pass

class Meta:
orders = ['status']

这个想法是为了模板有一些通用如{{for taskbox in taskboxes}}中的标签为每个任务栏创建一个单独的div,将保存该框的记录的表。我的问题是构建视图函数...

 #views.py 

def display_prdInboxEntry(request ,id):
如果request.method =='POST':
form = PrdInboxForm(request.POST)
如果form.is_valid():
form.save()
return HttpResponseRedirect('taskmanager / display /'+ id +'/')
else:
form = PrdInboxForm(request.POST)
return HttpResponseRedirect('taskmanager / display / '+ id +'/')

else:
form = PrdInboxForm()
user = request.user
** taskboxes = TaskBox.objects.all )
records_1 = InboxEntry.objects.filter(taskboxes [id] = 1)
records_2 = InboxEntry.objects.filter(taskboxes [id] = 2)
records_3 = InboxEntry.objects.filter (taskboxes [id] = 3)
..... **

返回render_to_response('taskmanager / taskmanager_view.html',{'form':form,任务箱':taskboxes'record_1':records_1,'records_2':records_2,'records_3':records_3,'user':user},context_instance = RequestContext(request))
pre>

InboxEntry模型有一个名为box的字段,只是对TaskBox模型的引用。我需要一种方法来映射说... TaskBox id 1与box = 1的所有InboxEntry对象,以便我可以适当地填充模板。我可以构建这个函数来适应这个,还是我完全错误的方法呢?

解决方案

听起来您正在寻找自动生成的反向查找属性。您可以获得与 TaskBox InboxEntries QuerySet >像这样:

  TaskBox.objects.filter(id = 1).inboxentry_set.all()

请参阅相关对象的文档


I have a bunch of message records that I would like to assign to different taskboxes.

#models.py

class TaskBox(models.Model):
    name = models.CharField(max_length=64, blank=False)

    def __str__(self):
        return u'%s' % (self.name)

    class Admin:
        pass

class InboxEntry(models.Model):
    job_number = models.CharField(max_length=14, unique=False, blank=False, null=False)
    job_name = models.CharField(max_length=64, unique=False, blank=False, null=False)
    request = models.CharField(max_length=64, choices=PRINT_CHOICES, blank=True, null=True)
    date_due = models.DateTimeField(("Due"),auto_now=False)
    note = models.TextField(max_length=1000, unique=False, blank=True, null=True)
    assigned_by = models.ForeignKey(UserProfile, blank=False, null=False)
    box = models.ForeignKey(TaskBox)
    assigned_to = models.ManyToManyField(UserProfile, related_name='name', blank=True)
    status = models.CharField(max_length=30, choices=STATUS_CHOICES, default="Awaiting Action")

    def __str__(self):
        return u'%s %s' % (self.job_number, self.job_name)

    class Admin: 
        pass

    class Meta:
        ordering = ['status']

The idea is for the template to have some generic tags like {{ for taskbox in taskboxes }} to create a separate div for each taskbox that will hold a table for that box's records. My problem is constructing the view function...

#views.py

def display_prdInboxEntry(request, id):
    if request.method == 'POST':
        form = PrdInboxForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('taskmanager/display/'+ id +'/')
        else:
            form = PrdInboxForm(request.POST)
            return HttpResponseRedirect('taskmanager/display/'+ id +'/')

    else:
        form = PrdInboxForm()
        user = request.user
        **taskboxes = TaskBox.objects.all()
        records_1 = InboxEntry.objects.filter(taskboxes[id]=1)
        records_2 = InboxEntry.objects.filter(taskboxes[id]=2)
        records_3 = InboxEntry.objects.filter(taskboxes[id]=3)
        ..... **

        return render_to_response('taskmanager/taskmanager_view.html', {'form': form, 'taskboxes': taskboxes, 'records_1' : records_1, 'records_2' : records_2, 'records_3' : records_3, 'user': user}, context_instance=RequestContext(request))

The InboxEntry model has a field called "box" that's just a reference to the TaskBox model. I need a way to map say... TaskBox id 1 with all of the InboxEntry objects with "box = 1" so that I can populate the templates appropriately. Can I construct the function to accommodate this, or am I going about it the wrong way entirely?

解决方案

It sounds like you're looking for the automatically-generated attribute for reverse lookups. You can get a QuerySet of all InboxEntries associated with a TaskBox like this:

TaskBox.objects.filter(id=1).inboxentry_set.all()

See the documentation on related objects.

这篇关于如何将一个模型对象映射到视图中的另一个模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆