A
A
Alexander Leyba2019-08-13 11:41:54
Django
Alexander Leyba, 2019-08-13 11:41:54

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))

Model:
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)

At the moment, you can vote as many times as you like.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura Khlyan, 2019-08-13
@Alex_shady

Set unique_together

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question