Всем привет! Помогите разобраться с PK. А именно, нужно сделать редактирование последних данных(meter_1-meter_5), для конкретного пользователя.
Заранее спасибо!
Model
class AddMeterData(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
meter_1 = models.IntegerField()
meter_2 = models.IntegerField()
meter_3 = models.IntegerField()
meter_4 = models.IntegerField()
meter_5 = models.IntegerField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
view
class MeterUpdateView(UpdateView):
model = AddMeterData
form_class = AddMeterForm
template_name = 'add_meters/update.html'
fields = ['meter_1', 'meter_2', 'meter_3', 'meter_4', 'meter_5']
success_url = reverse_lazy('meters:main')
context_object_name = 'meter'
def get_object(self, queryset=None):
try:
obj = AddMeterData.objects.filter(user=self.request.user).latest('created')
return obj
except AddMeterData.DoesNotExist:
raise Http404('Meter data not found')
except ValueError:
raise Http404('Invalid meter data id')
url
path('<int:pk>/update/', MeterUpdateView.as_view(), name='update'),
ссылка на template
<a href="{% url 'meters:update' meter.pk %}" class="btn btn-outline-light">Update</a>
error
NoReverseMatch at /
Reverse for 'update' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<pk>[0-9]+)/update/\\Z']