In my UserID model are a couple of foreign keys. In my UserIDForm I changed the widget for these fields to TextInput widgets and now I am trying to use that input and the get_or_create() method to either get or create the correspoding objects.
class Group(models.Model):
group_name = models.CharField(max_length=30, verbose_name='Gruppe')
class Room(models.Model):
room_name = models.CharField(max_length=30, verbose_name='Raum')
class UserID(models.Model):
userid_name = models.CharField(max_length=30, verbose_name='UID')
room = models.ForeignKey(Room, on_delete=models.SET_NULL, null=True)
group = models.ForeignKey(Group, on_delete=models.SET_NULL, null=True)
location = models.ForeignKey(Location, on_delete=models.SET_NULL, null=True)
class UserIDForm(ModelForm):
class Meta:
model = UserID
fields = ('userid_name', 'group', 'room', 'location',)
labels = {
'group': 'Gruppe',
'room': 'Raum',
'location': 'Standort',
}
widgets = {
'group': forms.TextInput(),
'room': forms.TextInput(),
}
def clean(self):
group_data = self.cleaned_data.pop('group')
group, created = Group.objects.get_or_create(group_name=group_data)
self.cleaned_data.update({'group': group})
room_data = self.cleaned_data.pop('room')
room, created = Room.objects.get_or_create(room_name=room_data)
self.cleaned_data.update({'room': room})
But when I submit the form I get the following error:
group_data = self.cleaned_data.pop('group')
KeyError: 'group'
I’m new to django and programming in general so I might be missing something very obvious but any help would be highly appreciated.