Answer the question
In order to leave comments, you need to log in
How to make a selection on a specific field in the database?
Good afternoon, I'm new to django and faced such a trivial problem, I'm trying to implement the functionality of voting by ip addresses, i.e. so that from one ip it would be possible to vote 1 time in one nomination. But I can't get the selection right.
view:
def post(self, request, pk, pk2):
client_ip, is_routable = get_client_ip(request)
nomination = NominationsModel.objects.get(id=pk2)
member = MemberModel.objects.get(id=pk)
chek = CheckModel.objects.order_by('ip')
print(chek)
if client_ip not in chek:
ip_save = CheckModel.objects.create(ip=client_ip, nominations=nomination)
member.check += 1
ip_save.save()
member.save()
return redirect('/competition/nominations/{}'.format(nomination.id))
class NominationsModel(models.Model):
"""Номинации"""
name = models.CharField('Название номинации', max_length=500)
def get_absolute_url(self):
return reverse('detailnominations', kwargs={'pk': self.id})
def __str__(self):
return self.name
def get_all(self):
return MemberModel.objects.filter(parent=self)
class Meta:
verbose_name = 'Номинацию'
verbose_name_plural = 'Номинаиции'
class CheckModel(models.Model):
ip = models.CharField('ip', max_length=100, blank=True, null=True)
nominations = models.ForeignKey(NominationsModel, on_delete=models.CASCADE, blank=True, null=True)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question