Create a ListView and DetailView with two models

2

I have 2 models that are related to a OneToOneField , but I do not know how to access the 2 models, my view is as follows:

class ListaSolicitudes(ListView):
    model = Modelo1 #aqui recibe un solo modelo, como uso el segundo modelos
    template_name = 'listar.html'

class DetalleView(TemplateView):
    template_name = 'detalles_solicitud.html'

    def get_context_data(self, *args, **kwargs):
         context = super(DetalleView, self).get_context_data(**kwargs)
         context['modelo1'] = Modelo1.objects.get(id=int('pk'))
         context['modelo1'] = Modelo2.objects.get(id=int('pk'))
         return context

When I want to enter this URL, it marks me an error:

ValueError at /registrar/detalle/1/
invalid literal for int() with base 10: 'pk'

I already tried with string e int , but it marks me error when doing QuerySet , my URLs are:

app_name = "agregar"

urlpatterns = [
    url(r'nuevo1/$', views.Model1CreateView.as_view(), name='formulario1'),
    url(r'nuevo2/(?P<pk>\d+)/$', views.Model2CreateView.as_view(), name='formulario2'),
    url(r'detalle/(?P<pk>\d+)/$', views.DetalleView.as_view(), name='detalles')
]

And in my command template I call the data like this:

Tu nombre: {{ Modelo1.nombre }}
Tus apellidos: {{ Modelo1.apellidos }}

Tu CURP: {{ Modelo2.curp }}
tu Nacionalidad: {{ Modelo2.nacionalidad }}

I add my models

class Modelo1(models.Model):
    nombre = models.CharField(max_length=20)
    apellidos = models.CharField(max_length=50)

    def __unicode__(self):
        return u"Número de solicitud: %s" % self.id

    def get_absolute_url(self):
        return u'/registrar/nuevo2/'


 class Modelo2(models.Model):
    relacionModelo1 = models.OneToOneField(Modelo1, blank=False, null=False, on_delete=models.CASCADE)
    curp = models.CharField(max_length=18)
    nacionalidad = models.CharField(max_length=50)

    def __unicode__(self):
        return "CURP: %s" % self.curp
    
asked by Kuroi 07.03.2018 в 18:09
source

1 answer

2

The main error, the one you show in your question:

ValueError at /registrar/detalle/1/
invalid literal for int() with base 10: 'pk'

It is because int() needs a value that can be converted to integer and 'pk' is an invalid value. If you want to use the PK that you see, you have to do it in the following way:

class DetalleView(TemplateView):
    template_name = 'detalles_solicitud.html'

    def get_context_data(self, *args, **kwargs):
         # El pk que pasas a la URL
         pk = self.kwargs.get('pk')
         context = super(DetalleView, self).get_context_data(**kwargs)
         context['modelo1'] = Modelo1.objects.get(pk=pk)
         context['modelo2'] = Modelo2.objects.get(pk=pk)
         return context

Keep in mind that the pk that is being used is not the same for both models. Is that what you want to achieve?

Update

Now that I can see your models, what you can do is use the inverse relationship from Modelo1 to Modelo2 :

class DetalleView(TemplateView):
    template_name = 'detalles_solicitud.html'

    def get_context_data(self, *args, **kwargs):
         # Asumiendo que el PK que estás pasando es del Modelo1
         pk = self.kwargs.get('pk')
         context = super(DetalleView, self).get_context_data(**kwargs)
         context['modelo1'] = Modelo1.objects.get(pk=pk)
         return context

And in the template:

Tu nombre: {{ modelo1.nombre }}
Tus apellidos: {{ modelo1.apellidos }}

Tu CURP: {{ modelo1.modelo2.curp }}
tu Nacionalidad: {{ modelo1.modelo2.nacionalidad }}
    
answered by 07.03.2018 / 19:25
source