I have a CreateView
view to which I am passing a PK by the URL,
what I want to do basically is to take that station and send it to a field of the model with which this view works (the form does not contain the model field to which I want to pass the PK, because I want to take it from the URL and send it to the model without entering the value for form
).
This is the view:
class EventTrackingCreateView(CreateView):
model = EventTracking
template_name = 'tracking/form/form.html'
form_class = EventTrackingForm
def get_context_data(self, **kwargs):
context = super(EventTrackingCreateView, self).get_context_data(**kwargs)
context['current_date'] = datetime.datetime.now()
return context
def get_success_url(self):
return reverse('events.list')
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(EventTrackingCreateView, self).dispatch(*args, **kwargs)
The URL of the view:
url(r'^tracking/create/(?P<pk>\d+)/$', login_required(views.EventTrackingCreateView.as_view()), name='events.tracking.create'),
The model: (the station must be saved in the event
field)
class EventTracking(TimeStampedModel):
type = models.ForeignKey(TypeEventTracking, null=True, blank=True, verbose_name=_('tipo de seguimiento'))
event = models.ForeignKey(Event, null=True, blank=True, verbose_name=_('evento'))
description = models.TextField(max_length=250, verbose_name=_('Descripcion'), validators=[MinLengthValidator(20)])
created_by = models.ForeignKey(User, null=True, blank=True, related_name="user_profile_created", verbose_name="_('creado_por)", on_delete=models.PROTECT)
updated_by = models.ForeignKey(User, null=True, blank=True, related_name="user_profile_updated", verbose_name="_('actualizado_por)", on_delete=models.PROTECT)
class Meta:
verbose_name = _('Seguimiento de Evento')
verbose_name_plural = _('Seguimiento de Eventos')
def save(self, *args, **kwargs):
return super(EventTracking, self).save(*args, **kwargs)
This view is being entered by means of a link in this way:
<a href="{% url 'events.tracking.create' event.id %}">Crear seguimiento</a>
It should be noted that I have a model called Event
as you have already seen, to which I want to associate a tracking, in the form I only render the fields type
and description
.
I was reading a bit about the method get_object
that I thought is the closest to solving the problem I have, I do not know if I'm right.