我有3个模型,其中2个属于ManyToManyField
class Vkgroupstomonitor(models.Model):
vk_groups = (('https://vk.com/southitpark','https://vk.com/southitpark'), ('https://vk.com/graphgrail', 'https://vk.com/graphgrail'))
vk_group_name= models.CharField(max_length=255, default='0')#, choices=vk_groups, default='0')
def __str__(self):
return self.vk_group_name
class Sourcetomonitor(models.Model):
source_names = (('Вконтакте', 'Вконтакте'), ('Facebook', 'Facebook'), ('Twitter (скоро)', 'Twitter (скоро)'))
source_name = models.CharField(max_length=255, choices=source_names, default='0')
def __str__(self):
return self.source_name
class Userproject(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="project", verbose_name=_("Владелец проекта"))
vk_groups = models.ManyToManyField(Vkgroupstomonitor, null=True, blank=True, related_name="vkgroups", verbose_name=_("Группы Вконтакте"))
source_name = models.ManyToManyField(Sourcetomonitor, null=True, blank=True, related_name="sourcename", verbose_name=_("Название источника"))
name = models.CharField(u'Название проекта', unique=True, max_length=255)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('userproject_edit', kwargs={'pk': self.pk})
在forms.py中我有modelform:
class UserprojectForm(forms.ModelForm):
class Meta:
model = Userproject
fields = ['name', 'user', 'description', 'vk_groups', 'source_name', 'date_updated', 'date_until'] #'date_created', cannot add django.core.exceptions.FieldError: Unknown field(s) (date_created) specified for Userproject
def __init__(self, *args, **kwargs):
super(UserprojectForm, self).__init__(*args, **kwargs)
self.fields['vk_groups'].queryset = Userproject.objects.filter(vk_groups=self.instance.vk_groups)
那么如何改变这一行 self.fields [' vk_groups']。queryset = Userproject.objects.filter(vk_groups = self.instance.vk_groups)在多选字段中获取Vkgroupstomonitor实例仅限于当前的Userproject ?
尝试了Userproject.objects.filter(vk_groups = self.instance.vk_vk_group_name)和Userproject.objects.filter(vk_group_name = self.instance.vk_groups)但另一个错误:"无法解析关键字' vk_group_name&# 39;进入田野。选项包括:date_created,date_until,date_updated,description,id,name,source_name,user,user_id,vk_groups"
现在它显示了所有选项,我想在创建和更新视图中使用admin和frontend。