将基于类的视图中的主键保存到另一个视图(Django) [英] Saving primary key in class based view to another view (django)

查看:50
本文介绍了将基于类的视图中的主键保存到另一个视图(Django)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什至不知道该如何找到解决方案,但让我们从头开始.

I do not know how even find solution for this, but lets start from the begining.

这些是我的模特

class Animal(models.Model):
SPECIES = (
    ("DOG", "DOG"),
    ("CAT", "CAT"),
)
species = models.CharField(max_length=4, choices=SPECIES)
name = models.CharField(max_length=20)
weight = models.PositiveSmallIntegerField(null=False, blank=False)
age = models.PositiveSmallIntegerField(null=False, blank=False)
color = models.CharField(max_length=10)
isill = models.BooleanField(null=False)
isagressive = models.BooleanField(null=False)
isadopted = models.BooleanField(null=False)
isreturned = models.NullBooleanField()
whichbox = models.CharField(max_length=5)
photo = models.ImageField(null=True, blank=True)

def __str__(self):
    return self.name


class MedicalHistory(models.Model):
animal = models.ForeignKey(Animal, on_delete=models.CASCADE)
disease = models.CharField(max_length=100)
medicine = models.CharField(max_length=20, null=True, blank=True)
therapy = models.CharField(max_length=50)
howmuchmed = models.CharField(max_length=50)
daterecord = models.DateField

def __str__(self):
    return self.disease

这是我的网址:

urlpatterns = [
    path('', AnimalListView.as_view(template_name='Animals/animals.html'), name='animallist'),
    path('add/', AddAnimal.as_view(), name='addanimal'),
    path('edit/<int:pk>/', EditAnimal.as_view(), name='editanimal'),
    path('detail/<int:pk>/', AnimalDetailView.as_view(template_name='Animals/animaldetail.html'), name='animaldetail'),
    path('medlist/<int:pk>/', MedhistoryListView.as_view(template_name='Animals/medlist.html'), name='medlist'),
]

和我的观点(所有观点中只有两个)

and my views (only two from all)

class AnimalDetailView(DetailView):
queryset = Animal.objects.all()

def get_object(self):
    object = super().get_object()
    object.save()
    return object
context_object_name = 'animal_detail'


class MedhistoryListView(ListView):

"PLACE FOR CODE"

context_object_name = 'medical_history_list'

在MedhistoryListView中,我想展示动物患有的疾病.AnimalDetailView模板上有指向Medhistorylistview的URL的链接.我的主要问题是如何将主键从一个视图保存到另一个视图,并仅通过指定animal.pk选择这些对象.就像MedicalHistory.objects.get(animal.pk = pk).谁能帮我吗?

In the MedhistoryListView i would like to show a diseases that animal had. There is the link on AnimalDetailView template to the url with Medhistorylistview. My main problem is how to save primary key from one view to another and choose only these objects with specify animal.pk. Like a MedicalHistory.objects.get(animal.pk=pk). Can anyone help me?

推荐答案

首先,在AnimalDetailView中删除该 get_object()方法.除了引起完全毫无意义的更新查询外,它什么也没做.

Firstly, delete that get_object() method in your AnimalDetailView. It is doing nothing other than causing a completely pointless update query.

现在,在您的MedhistoryListView中,您要将查询集设置为与URL中PK标识的动物相关的对象集.因此,定义 get_queryset()方法:

Now, in your MedhistoryListView, you want to set the queryset to be the set of objects related to the Animal identified by the PK in the URL. So, define the get_queryset() method:

def get_queryset(self, *args, **kwargs):
    return MedicalHistory.objects.filter(animal_id=self.kwargs['pk'])

这篇关于将基于类的视图中的主键保存到另一个视图(Django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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