0

I am building a user model and wanna attach it to a modelform as below shown. How can I get the email of each user by shell accessing the mysql database?

I have tried these to get the data by shell, but the previous one said the object has no email attribute and the latter one said the forms.py has no object.

from Project.User.models import UserProfile as p
p.objects.filter(is_active=True).first().email //    
from Project.User.forms import ClientProfileForm as p
p.objects.filter(is_active=True).first().email 

Code:

models.py:

class UserProfile(BaseModel):
    user = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE, verbose_name='client')
    name = models.CharField('name',max_length=30, null=False, help_text="Name")
    gender = models.CharField('sex',max_length=1, help_text="Sex")

    class Meta:
        verbose_name = 'Client'
        verbose_name_plural = 'Client'

    def __str__(self):
        return 'client:%s' % self.user

forms.py:

class ClientProfileForm(forms.ModelForm):
    email = forms.EmailField()

    class Meta:
        model = UserProfile
        fields = ("user", "name", "gender")
WILLIAM
  • 457
  • 5
  • 28

3 Answers3

2

Have you tried using

p.objects.filter(is_active=True).first().user.email
Sanip
  • 1,772
  • 1
  • 14
  • 29
  • You are welcome @WILLIAM. To save a new User's data including the email, you might need to override save function of the UserProfile model. – Sanip Feb 18 '19 at 09:16
1

There is no data in form before you send a data to it so you can't get the user and it's email from your shell.

Forms are used to get data from HTML templates for example and validate that data and then add those data to the database.

If you fill a form, you can access the data that has been cleaned from your view like this:

form = ClientProfileForm(request.POST)
if form.is_valid():
    print(form.cleaned_data['my_form_field_name'])

Also you can set some default values for forms. Example here:

Django forms initial example on stackoverflow

And finally for your problem:

You are trying to access email from UserProfile which doesn't have a email field but the User model does.

so you can access the email like this:

from Project.User.models import UserProfile as p

# Get the first profile and then get the email from user model
p.objects.filter(is_active=True).first().user.email 
Navid Zarepak
  • 4,148
  • 1
  • 12
  • 26
  • Thank you. It works. But one more question, How can I save a new User's data including the email? Can I create a method which is like "p = UserProfile() p.name=name ..... p.save()"? – WILLIAM Feb 18 '19 at 09:17
  • There is few ways. You can override the form's save method by defining a `save` function in your form and add the user email there. check here for an example: https://stackoverflow.com/questions/3927305/django-how-to-override-form-save – Navid Zarepak Feb 18 '19 at 09:26
  • There is few ways. You can override the form's save method by defining a `save` function in your form and add the user email there. check here for an example: https://stackoverflow.com/questions/3927305/django-how-to-override-form-save – Navid Zarepak Feb 18 '19 at 09:26
-1

from Project.User.forms import UserProfile as p

This line is incorrect because the name of your form is ClientProfileForm and not UserProfile. This is why you are getting the error forms.py has no object

Tajinder Singh
  • 1,361
  • 2
  • 14
  • 26
  • Sorry, its a typo. My actual command is "from Project.User.forms import ClientProfileForm as p" which return the error as "AttributeError: type object 'ClientProfileForm' has no attribute 'objects'". Actually, I cannot figure out how the data structured. The modelform (email) is attached with the model but I cannot find the email from model. – WILLIAM Feb 18 '19 at 09:07