File: func_noerror_foreign_key_sets.py

package info (click to toggle)
pylint-django 2.0.13-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: python: 1,807; sh: 13; makefile: 5
file content (32 lines) | stat: -rw-r--r-- 777 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
Checks that Pylint does not complain about foreign key sets on models
"""
#  pylint: disable=missing-docstring

from django.db import models


class SomeModel(models.Model):
    name = models.CharField(max_length=20)

    def get_others(self):
        return self.othermodel_set.all()

    def get_first(self):
        return self.othermodel_set.first()


class OtherModel(models.Model):
    count = models.IntegerField()
    something = models.ForeignKey(SomeModel, on_delete=models.CASCADE)


class ThirdModel(models.Model):
    whatever = models.ForeignKey(SomeModel, related_name='whatevs',
                                 on_delete=models.CASCADE)


def count_whatevers():
    if SomeModel().whatevs.exists():
        return SomeModel().whatevs.count()
    return -1