my doubt is, how to display the data through a ListView through a ForeignKey, I am trying to display the results in this way, but nothing appears and it seems strange to me that the context_object_name does not work when placing the name in the template. I don't know how I can make it display the information in the template. Can you help me?:
EarningsReport/models.py
class ReporteGanancias(models.Model):
mano_obra=models.ForeignKey(ManoObra, on_delete=models.SET_NULL, null=True)
parte=models.ForeignKey(Inventory, on_delete=models.SET_NULL, null=True)
def __str__(self):
#return f'{self.parte} {self.mano_obra} '
inventory/models.py
class Inventory(models.Model):
STATUS = (
('Ok', 'Ok'),
('Pending', 'Pending'),
)
dealer = models.CharField(max_length=255, blank=True, null=True)
codigoInventory=models.CharField(max_length=255,blank=True)
invoiceNumber=models.IntegerField()
descriptionInventory= models.CharField(max_length=255, blank=True, null=True)
quantityInventory=models.IntegerField(default=0)
unitPriceInventory=models.IntegerField()
minimumInventory=models.IntegerField()
# invoice_number=models.IntegerField()
status=models.CharField(max_length=255,choices=STATUS,default='Ok')
fecha_registro = models.DateTimeField(default=datetime.now)
def __str__(self):
return f'{self.dealer}: {self.codigoInventory} {self.invoiceNumber} {self.descriptionInventory} ' \
f'{self.quantityInventory} {self.unitPriceInventory}{self.minimumInventory}{self.status}{self.fecha_registro}'
EarningReport/views.py
class pendingStock(ListView):
model=ReporteGanancias
template_name = 'ReporteGanancias/reports-pending-stock.html'
context_object_name='stocks'
queryset=ReporteGanancias.objects.all()
EarningsReport/reports-pending-stock.html'
{% for stock in stocks %}
<tr>
<td>{{ stock.codigoInventory }}</td>
<td>Part Name</td>
<td>{{ stock.unitPriceInventory }}</td>
<td><a href="#" class="stock-quantity" data-type="text" data-pk="1">{{ stock.quantityInventory }}</a></td>
<td>{{ stock.dealer }}</td>
<td>{{ stock.invoiceNumber }}</td>
</tr>
{% endfor %}
How do you accept null in your 'part' field
It is necessary to verify first in the template that the relationship exists, after this you can show the fields if the relationship exists.
The correct way to access the ForeignKey data is like this:
I complete the answer with your last comment:
You get this error if you remove the null=True
This is because in your on_delete you have it set to models.SET_NULL, this means that deleting the related object sets the field to NULL, the problem is that if you remove null=True then you don't allow the null value. If you want to use on_delete=models.SET_NULL you absolutely need to have null=True, if you don't want to accept null then you need to set on_delete to a different value like CASCADE, PROTECT or RESTRINCT, you can read a bit about each of these possible values here: https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.ForeignKey.on_delete
If this answer helped you, please mark it as accepted for future reference.