user13080604
user13080604

Reputation:

Why can't I display more fields in django?

I'm Trying to develop a social website, and I want to display the bio and blood group of the user on their profile page along with their name and email. While the name and email are being Displayed in the profile page, their bio and blood group are not being displayed although I wrote the same code for them as their name and email. Can anyone please help me out?

My models.py :

from django.db import models
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

my forms.py :

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm



class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()
    CHOICES = (
        ('type', 'AB+'),
        ('type', 'AB-'),
        ('type', 'A+'),
        ('type', 'A-'),
        ('type', 'B+'),
        ('type', 'B-'),
        ('type', 'O+'),
        ('type', 'O-'),
        )
    bloodgroup = forms.CharField(widget=forms.Select(choices=CHOICES))
    bio = forms.CharField()


    class Meta:
        model = User
        fields = ['username', 'email', 'bio', 'bloodgroup', 'password1', 'password2']

my profile.html:

{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
      <div class="media">
        <img class="rounded-circle account-img" src="{{ user.profile.image.url }}">
        <div class="media-body">
          <h2 class="account-heading">{{ user.username }}</h2>
          <p class="text-secondary">{{ user.email }}</p>
          <p class="text-primary">{{ user.bio }}</p>
          <p2 class="text-info">Blood Group - {{ user.bloodgroup }}</p2>
        </div>
      </div>
      <!-- FORM HERE -->
    </div>
{% endblock content %}

Upvotes: 1

Views: 324

Answers (3)

Manan M.
Manan M.

Reputation: 1394

For that you have to extend AbstractUser as below and add extra field as per your requirements...

from django.db import models
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    bloodgroup = models.CharField(name="bloodgroup", max_length=10, null=True, blank=True)
    bio = models.CharField(name="bio", max_length=100, null=True, blank=True)

And suppose this User model is in app named app_1 then you have to set AUTH_USER_MODEL in settings.py as below...

settings.py

AUTH_USER_MODEL = "app_1.User"

Run makemigrations and migrate commmand because we changed in model.

And your form should be as below...

from django import forms

class UserRegisterForm(forms.ModelForm):
    email = forms.EmailField()
    CHOICES = (
        ('type', 'AB+'),
        ('type', 'AB-'),
        ('type', 'A+'),
        ('type', 'A-'),
        ('type', 'B+'),
        ('type', 'B-'),
        ('type', 'O+'),
        ('type', 'O-'),
        )
    bloodgroup = forms.CharField(widget=forms.Select(choices=CHOICES))
    bio = forms.CharField()


    class Meta:
        model = User
        fields = '__all__'

Upvotes: 0

Rishit Aghera
Rishit Aghera

Reputation: 163

Default User model does not contains bloodgroup and bio fields, So you just need to inherit User(AbstractUser) model in your model.py and define only new fields in that class, rest of default fields will be inherited. After that you can assign attributes to fields in forms.

Try this and let me know if its working or not..

Upvotes: 3

Mr.Robot
Mr.Robot

Reputation: 333

django.contrib.auth.models User model does not contain any fields called bio or blood-group and you're using. You need to inherit the User model and create a custom model with these 2 attributes.

When you use user.username and user.email. It fetches from the User object you passed to the template.

from django.contrib.auth.models import AbstractUser
class Profile(AbstractUser):
    REQUIRED_FIELDS = ['username']
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    bio = models.CharField(max_length=14, blank=True, default=None)
    bloogroup = models.CharField(max_length=14, blank=True, default=None)

and for forms use

class UserRegisterForm(ModelForm):
   class Meta:
       model = Profile
       fields = '__all__'

Upvotes: 0

Related Questions