Reputation: 133
I have a model called group and another called group membership. What I'm trying to do is return a seperate string, for one of my fields in my form. It displays correctly in the front end but when writing to the database it's all in one string. All im trying to do is get the group_name and cut out the rest of the string when writing to the database. I'm aiming for a way to return more then one value, because i need to grab company and department later on from that same model.
My Model
# 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)
My Form
# 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')
Upvotes: 0
Views: 5600
Reputation: 630
# 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
To use the @property
:
from <yourapp>.models import Group
obj = Group()
# following line will print the special string
obj.get_special_combination_value
The property value can also be used in django admin as:
class GroupAdmin(admin.ModelAdmin):
list_display = ('get_special_combination_value',)
admin.site.register(Group, GroupAdmin)
Upvotes: 3