I have a foreign key field in my model which should be set from the url params when the model is saved.I need to set the field automaticatically from the registration-id
models.py
class Mymodel(BaseModel):
name = models.CharField()
created_by = models.ForeignKey(
'User',
related_name='',
null=True,
blank=True,
on_delete=models.SET_NULL
)
last_edited = models.DateField(auto_now=True)
registration = models.ForeignKey(
SomeModel,
related_name='',
null=True,
blank=True,
on_delete=models.SET_NULL
)
views.py
class MyCreateView(LoginRequiredMixin, CreateView, CustomPermissionMixin):
form_class = ''
pk_url_kwarg = ''
template_name = 'c.html'
def form_valid(self, form):
form.instance.created_by = self.request.user.person
return super().form_valid(form)
def get_success_url(self):
return reverse('')
forms.py
class MyModelForm(forms.ModelForm):
class Meta:
model = Mymodel
fields = ('name',)
urls.py
'<int:registration_id>/employee/create/'