cudris
cudris

Reputation: 23

django- createview, get id with which it was saved in database return "None", self.object.pk in form_valid is "None"

I have created a view with createview where I have a form, this view has the url: path('asignacionContact/<int:pk>/',AsignacionCreateView.as_view(), name='assignacion_contact') , I fill the form and effectively it is saved in the database, but I want to get the id with which it was saved in the database, so in the form_valid method I try to get the id with self.object.pk or form.instance.pk but it returns None , how can I fix this?

urls.py

app_name = 'gestionAsignacion'

urlpatterns = [
     
    path('asignacionContact/<int:pk>/',AsignacionCreateView.as_view(), name='asignacion_contact'),
    path('detalle/asignacion/<int:pk>',descargarAsignacion.as_view(), name='descargar_asignacion'),
    path('asignacionGeneral/',asignacionGeneral.as_view(), name='asignacion_general'),
    
    
]

model.py

class AsignacionContact(models.Model):
    id = models.IntegerField(primary_key=True)
    idasignacion_general = models.ForeignKey('AsignacionGeneral', models.DO_NOTHING, db_column='idasignacion_general')
    nombre_asignacion = models.CharField(max_length=100)
    fecha_inicio = models.DateField()
    fecha_fin = models.DateField()
    observacion = models.CharField(max_length=255, blank=True, null=True)
    estado = models.IntegerField(blank=True, null=True)

    def __str__(self):
        return self.nombre_asignacion
    
    # def get_absolute_url(self):
    #     print('reverse')
    #     return reverse('gestionAsignacion:asignacion_contact',kwargs={ 'pk': self.pk })

    class Meta:
         managed = False
         db_table = 'asignacion_contact'

view.py

class AsignacionCreateView(CreateView):
    model=AsignacionContact
    form_class=AsignacionForm
    template_name='gestionAsignacion/asignacion_contact.html'
    # success_url = reverse_lazy('gestionAsignacion:asignacion_general')
    
    

    def form_valid(self, form):
        
        isvalid = super().form_valid(form)
        print('form_valid')
        print(form.instance.pk)
        print(self.object.pk)
        return isvalid
        
    
   
    def post(self, request, *args, **kwargs):
        print('post')
        return super().post(request, *args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(AsignacionCreateView, self).get_form_kwargs()
        kwargs['pk'] = self.kwargs.get('pk')
        return kwargs

    def get_success_url(self):
        print('hola succes')
        print(self.object.id)
        return reverse('gestionAsignacion:asignacion_general')

Upvotes: 0

Views: 335

Answers (1)

Carewen
Carewen

Reputation: 183

As per Django documentation you can save and get the id of the save.

>>> b2 = Blog(name='Cheddar Talk', tagline='Thoughts on cheese.')
>>> b2.id     # Returns None, because b2 doesn't have an ID yet.
>>> b2.save()
>>> b2.id     # Returns the ID of your new object

Upvotes: 1

Related Questions