假设我有两个类/模型:Hand
和Finger
。想象一下,指纹是一个TextField,而一个手在models.py
中的手指数是未知的:
from django.db import models
class Hand(models.Model):
smoothness = models.IntegerField(default=0,
validators=[
MaxValueValidator(5),
MinValueValidator(0)
])
fingers = models.ManyToManyField('Finger')
num_fingers = models.IntegerField(default=0)
has_thumb = models.BooleanField(default=False)
def save(self, *args, **kwargs):
self.num_fingers = check_num_finger() # How do I do that?
if check_has_thumb(): # How do I do that?
self.has_thumb = True
super(Hand, self).save(*args, **kwargs)
class Finger(model.Model):
is_a_thumb = models.BooleanField(default=False)
fingerprint = models.TextField()
新的Hand通过
admin.py
添加,如下所示:from django.contrib import admin
from .models import Hand, Finger
@admin.register(Finger)
class FingerAdmin(admin.ModelAdmin):
fieldsets = [('Finger', {'fields': ['is_a_thumb','fingerprint']})]
list_display = ('is_a_thumb', 'fingerprint',)
@admin.register(Hand)
class HandAdmin(admin.ModelAdmin):
fieldsets = [('Hand', {'fields': ['smoothness']},
('Fingers', {'fields': ['fingers']}]
并且保存
Hand
时,我们需要检查初始化时与Hand相关的手指数。还可以访问Finger模型中的is_a_thumb
字段,以从Hand填充/关联has_a_thumb
。我如何从每个
is_a_thumb
访问Finger
,以便如果有一个手指在is_a_thumb
位置,它会在保存之前更改Hand
的has_a_thumb
值我如何访问号码。分配给
Finger
的Hand
,以便在保存之前可以更新num_fingers
? 最佳答案
我不确定是否可以保存您的对象。而且我认为这不是正确的方法。我相信您需要做的是监听before
信号并更新您的手。
from django.db.models.signals import m2m_changed
def fingers_changed(sender, **kwargs):
#sender Hand.fingers.through (the intermediate m2m class)
#instance hand (the Hand instance being modified)
#action "pre_add" (followed by a separate signal with "post_add")
#reverse False (Hand contains the ManyToManyField, so this call modifies the forward relation)
#model Finger (the class of the objects added to the Hand)
#pk_set finger_ids beign added (when post_add its a set of all, not the new ones only, but all)
#using "default" (since the default router sends writes here)
if kwargs['action'] == 'post_add':
hand = kwargs['instance']
hand.num_fingers = hand.fingers.count() #or len(kwargs['pk_set'])
hand.has_thumb = hand.fingers.filter(is_a_thumb=True).exists()
hand.save()
m2m_changed.connect(fingers_changed, sender=Hand.fingers.through)
注意:顺便说一句,我不确定M2M在这里是否正确。是的,一只手可以有很多手指,但是一个手指只能属于一只手?如果这是对的,那么您需要将关系更改为一对多(手指要握住FK的手指)。
关于python - 保存模型前如何检查ManytoMany是否有值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37583355/