1

I want to put the email of user in my model Article.

from django.db import models
from django import forms
from django.contrib.auth.models import User
 class Article(models.Model):
 #auteur
 email = models.ForeignKey(User.email, unique=True)
 name = models.CharField(max_length=200)
 #titre
 title = models.CharField(max_length=200)
 title_en = models.CharField(max_length=200)
 subtitle = models.CharField(max_length=200)
 subtitle_en = models.CharField(max_length=200)
p.s.w.g
150k31 gold badges307 silver badges339 bronze badges
asked Aug 6, 2013 at 12:21

1 Answer 1

2

What you have written has no sense. You have to keep a ForeignKey on the user, and then access to his email through user.email:

class Article(models.Model):
 #auteur
 user = models.ForeignKey(User, unique=True)
 #titre
 title = models.CharField(max_length=200)
 title_en = models.CharField(max_length=200)
 subtitle = models.CharField(max_length=200)
 subtitle_en = models.CharField(max_length=200)

Then, Article.user.username (or self.user.username inside an Article method) gives you the name and Article.user.email the e-mail.

Some explanations

In Django, you can create ForeignKey only on Model object, and not object field. This will be translate in SQL as a classic reference between tables. (If you check your Article SQL database, you'll see a user_id field. This field will contains the ID of the user, extract from the auth_user table, which created the Article.

answered Aug 6, 2013 at 12:25
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, great answer with explanations!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.