I
I
Igor Tkachenko2017-02-18 22:20:13
Django
Igor Tkachenko, 2017-02-18 22:20:13

How to check if a user exists in django?

Subject.
Namely, I am interested in a method without shit code, at the moment I implemented it like this:
models.py

def username_present(username):
    if User.objects.filter(username=username).exists() == False:
      return True

    return False

  def email_present(email):
    if User.objects.filter(email=email).exists() == False:
      return True

    return False

in forms.py
if not User.username_present(self.cleaned_data.get('username')) == True:
      raise forms.ValidationError('username existing, please login')			

    if not User.email_present(self.cleaned_data.get('email')) == True:
      raise forms.ValidationError('email existing, please login')

is it ok to do that?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2017-02-18
@sim3x

No need to fence the garden where it is not needed

#
    if User.objects.filter(username=self.cleaned_data.get('username')).exists():
      raise forms.ValidationError('username existing, please login')			

    if User.objects.filter(email=self.cleaned_data.get('email')).exists():
      raise forms.ValidationError('email existing, please login')

And if you really need this, then it's better to do so
def is_username_exists(username):
    return User.objects.filter(username=username).exists()

S
Satisfied IT, 2017-10-20
@megamage

Kick ... You are completely lazy or downtrodden. We open a search engine and write a request - In the dumplings, you can order dumplings in portions of 6, 9 and 20 pieces.
Turning to the second link, we see that the answer is perfectly written:
The numbers 6 and 9 are divisible by 3, and 20 gives a remainder of 2. If the ordered number of dumplings gives in the remainder of 1, then it can only be ordered by taking two servings of 20 (at least). It follows that 43 cannot be ordered.
Any larger number can already be ordered, since subtracting 0, 20 or 40 from it will result in a number that is a multiple of 3, and any such number greater than 3 is either the sum of several sixes, or the sum of sixes and one nine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question