我有一个名为group的模型,另一个名为group membership。我尝试做的是为我的一个字段中的一个字段返回一个单独的字符串。 它在前端正确显示,但在写入数据库时,它全部在一个字符串中。所有我想要做的就是获取group_name并在写入数据库时删除其余的字符串。 我的目标是返回一个以上的值,因为我需要从同一个模型中获取公司和部门。
我的模特
# Group #
#########
class Group(models.Model):
created_by = models.ForeignKey('auth.User', related_name='group_created_by')
id = models.AutoField(primary_key=True)
company = models.ForeignKey(Company)
department = models.ForeignKey(Department)
group_name = models.CharField(max_length=100)
role = models.CharField(max_length=100)
status = models.CharField(max_length=8)
created_date = models.DateTimeField(default=timezone.now)
modified_date = models.DateTimeField(blank=True, null=True)
modified_by = models.ForeignKey('auth.User',
related_name='group_modified_by',
blank=True, null=True)
def create(self):
self.save()
def __str__(self):
return '{}{}{}'.format(self.company, self.department, self.group_name)
我的表格
# Group Name #
##############
class GroupNameModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
return '{}'.format(obj.group_name)
# Group Membership #
####################
class GroupMembershipForm(forms.ModelForm):
options = (('Enabled', 'Enabled',), ('Disabled', 'Disabled'))
member_id = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control'})
)
company = forms.ModelChoiceField(
queryset=Company.objects.filter(status='Enabled'),
widget=forms.Select(attrs={'class': 'form-control'}),
empty_label=''
)
department = forms.ModelChoiceField(
queryset=Department.objects.filter(status='Enabled'),
widget=forms.Select(attrs={'class': 'form-control'}),
empty_label=''
)
group_name = GroupNameModelChoiceField(
queryset=Group.objects.filter(status='Enabled'),
widget=forms.Select(attrs={'class': 'form-control'}),
empty_label=''
)
user_id = forms.ModelChoiceField(
queryset=Person.objects.filter(status='Enabled'),
widget=forms.Select(attrs={'class': 'form-control'}),
empty_label=''
)
full_name = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control'}),
)
status = forms.CharField(
widget=forms.Select(
attrs={'class': 'form-control'},
choices=options
)
)
class Meta:
model = GroupMembership
fields = ('member_id', 'company', 'department', 'group_name',
'user_id', 'full_name', 'status')
答案 0 :(得分:1)
# Group #
#########
class Group(models.Model):
created_by = models.ForeignKey('auth.User', related_name='group_created_by')
id = models.AutoField(primary_key=True)
company = models.ForeignKey(Company)
department = models.ForeignKey(Department)
group_name = models.CharField(max_length=100)
role = models.CharField(max_length=100)
status = models.CharField(max_length=8)
created_date = models.DateTimeField(default=timezone.now)
modified_date = models.DateTimeField(blank=True, null=True)
modified_by = models.ForeignKey('auth.User',
related_name='group_modified_by',
blank=True, null=True)
def create(self):
self.save()
# Please rename to something sensible
@property
def get_special_combination_value(self):
return '{}{}{}'.format(self.company, self.department, self.group_name)
def __str__(self):
return self.group_name
使用@property
:
from <yourapp>.models import Group
obj = Group()
# following line will print the special string
obj.get_special_combination_value
属性值也可以在django admin中用作:
class GroupAdmin(admin.ModelAdmin):
list_display = ('get_special_combination_value',)
admin.site.register(Group, GroupAdmin)