I am trying to return extra data for the "following_user" but I'm getting returned the data for the "user" instance
The View to get Users following list:
class UsersFollowing(generics.ListAPIView):
authentication_class = [authentication.TokenAuthentication]
permission_class = [permissions.IsAuthenticated]
serializer_class = FollowingSerializer
def get_queryset(self):
user = self.request.GET.get('user_id')
obj = Follow.objects.filter(user=user)
return obj
the Serializer:
class FollowingSerializer(serializers.ModelSerializer):
avi_pic = serializers.SerializerMethodField('get_avi_pic')
username = serializers.SerializerMethodField('get_username')
first_name = serializers.SerializerMethodField('get_first_name')
last_name = serializers.SerializerMethodField('get_last_name')
def get_username(self, obj):
username = obj.following_user.username
return username
def get_first_name(self, obj):
first_name = obj.following_user.first_name
return first_name
def get_last_name(self, obj):
last_name = obj.following_user.last_name
return last_name
class Meta:
model = Follow
fields = "__all__"
my Follow model:
class Follow(models.Model):
user = models.ForeignKey(
"User", related_name="follower", on_delete=models.CASCADE)
following_user = models.ForeignKey(
"User", related_name="following", blank=True, on_delete=models.CASCADE)
date_followed = models.DateTimeField(auto_now_add=True)
My User model:
class User(AbstractUser):
objects = UserManager()
avi_pic = models.ImageField(
_('avi_pic'), upload_to=aviFile, null=True, blank=True)
email = models.EmailField(max_length=250, unique=True)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
username = models.CharField(max_length=60, unique=True)
I think your naming of foreign key fields is not good. It is often better not to add the trailing the word id. Then the Django ORM will automatically adds the field named user_id.
class Follow(models.Model):
user = models.ForeignKey(
"User", related_name="follower", on_delete=models.CASCADE)
following_user = models.ForeignKey(
"User", related_name="following", blank=True, on_delete=models.CASCADE)
date_followed = models.DateTimeField(auto_now_add=True)
And then in the serializer, you could set the serializer of the User model.
class FollowingSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only = True)
following_user = UserSerializer(read_only = True)
class Meta:
model = Follow
fields = "__all__"
Related
I'm building a simple Lead Generator using Django Rest Framework.
I'm trying to show a list of "assigned facilities" inside a lead using django's many to many fields. But all it will show inside the API is the id of each of the facilities associated to the many to many field. How do I access more than just the name using DRF? I basically need to show the name, a description and a picture of the facility from each facility record.
serializers.py
class LeadUpdateSerializer(serializers.ModelSerializer):
is_owner = serializers.SerializerMethodField()
class Meta:
model = Lead
fields = (
"id",
"first_name",
"last_name",
"PrimaryAddress",
"assigned_facilities",
)
read_only_fields = ("id", "is_owner")
def get_is_owner(self, obj):
user = self.context["request"].user
return obj.agent == user
models.py
class Facility(models.Model):
UUID = models.CharField(max_length=150, null=True, blank=True)
Name = models.CharField(max_length=150, null=True, blank=False)
mainimage = models.ImageField(null=True, blank=True)
FacilityDescription = models.TextField(max_length=1000, null=True, blank=True)
def __str__(self):
return self.Name
class Lead(models.Model):
assigned_facilities = models.ManyToManyField(Facility, related_name='assigned_facilities')
created_at = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=40, null=True, blank=True)
last_name = models.CharField(max_length=40, null=True, blank=True)
def __str__(self):
return f"{self.first_name} {self.last_name}"
We can like below:
class FacilitySerializer(serializers.ModelSerializer)
class Meta:
fields = (
"id",
"Name",
"mainimage",
"FacilityDescription",
)
class LeadUpdateSerializer(serializers.ModelSerializer):
assigned_facilities = FacilitySerializer(many=True)
is_owner = serializers.SerializerMethodField()
class Meta:
model = Lead
fields = (
"id",
"first_name",
"last_name",
"PrimaryAddress",
"assigned_facilities",
)
read_only_fields = ("id", "is_owner")
def get_is_owner(self, obj):
user = self.context["request"].user
return obj.agent == user
I have my DRF app. In my case, one wallet can have many entries such as income or expense. When I call my endpoint (viewset) I get data in this format:
[
{
"id": "d458196e-49f1-42db-8bc2-ee1dba438953",
"owner": 1,
"viewable": [],
"entry": []
}
]
How can I get the content of "entry" variable?.
class Category(models.Model):
name = models.CharField(max_length=20, unique=True)
def __str__(self):
return self.name
class BudgetEntry(models.Model):
STATE= [
('income','income'),
('expenses','expenses'),
]
amount = models.IntegerField()
entry_type = models.CharField(max_length=15, choices=STATE, null=True)
entry_category = models.ForeignKey(Category, null=True, blank=True, on_delete=models.SET_NULL)
class WalletInstance(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='owner', on_delete=models.CASCADE)
viewable = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='can_view', blank=True)
entry = models.ManyToManyField(BudgetEntry, related_name='BudgetEntry', blank=True)
Serializers.py:
class BudgetEntrySerializer(serializers.ModelSerializer):
class Meta:
model = BudgetEntry
fields = '__all__'
class WalletInstanceSerializer(serializers.ModelSerializer):
owner = serializers.ReadOnlyField(source='owner.id')
class Meta:
model = WalletInstance
fields = '__all__'
Views.py:
class WalletViewset(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
serializer_class = WalletInstanceSerializer
def get_queryset(self):
user_id = self.request.user.id
available = WalletInstance.objects.filter(
Q(owner=user_id)
)
return available
Change your serializer like this:
class WalletInstanceSerializer(serializers.ModelSerializer):
owner = serializers.ReadOnlyField(source='owner.id')
entry = BudgetEntrySerializer(many=True, read_only=True)
class Meta:
model = WalletInstance
fields = '__all__'
Hi fellow programmers,
I am getting the following error when I try to visit my url can you please point out the mistake in my code. Is there any error in my serializer or view. I will be gateful if you find that.
Got AttributeError when attempting to get a value for field username on serializer PerformerSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the User instance.
Original exception text was: 'User' object has no attribute 'user'.
models.py
class User(AbstractBaseUser):
username = models.CharField(_('Username'), unique=True,max_length=20)
email = models.EmailField(_('Email address'), unique=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
last_login = models.DateTimeField(auto_now=True)
date_joined = models.DateTimeField(auto_now_add=True)
full_name = models.CharField(_("Full Name"), max_length=50,null=True)
date_of_birth = models.DateField(_("Date of birth"), auto_now=False, auto_now_add=False,null=True)
is_verified = models.BooleanField(_("Verified"), default=False)
profileImage = models.ImageField(_('Profile Image'),null=True, upload_to=None, height_field=None, width_field=None, max_length=100)
gender = models.CharField(_("Gender"), max_length=6, choices=GENDER_CHOICES,null=True)
profileTitle = models.CharField(_('Title'), max_length=20,null=True)
profileBio = models.TextField(_('Bio'),null=True)
profileWebsite = models.URLField(_('Website'), max_length=25,null=True)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email',]
objects = UserManager()
def has_perm(self,perm,obj=None):
return self.is_admin
def has_module_perms(self,app_label):
return True
def __str__(self):
return self.username
def natural_key(self):
return (self.username)
class Song(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(_("Name"), max_length=50)
song = models.FileField(_("Song"), upload_to=None, max_length=100)
song_image = models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100)
likes = models.IntegerField(_("Likes"),default=0)
views = models.IntegerField(_("Views"),default=0)
performer = models.ManyToManyField('self',verbose_name=_("Performed By"),related_name='artist_in_song')
def performer_user(self, username):
performer = Song.objects.get(user__username=username)
if not self.is_performer(performer):
self.performer.add(performer)
self.save()
return True
return False
def is_performer(self, username):
return self.performer.all().filter(user__username=username).exists()
serializers.py
class PerformerSerializer(serializers.ModelSerializer):
username = serializers.CharField(source='user.username')
class Meta:
model = Song
fields = ('username',)
class UserSerializer(serializers.ModelSerializer):
#id = serializers.ReadOnlyField(source='user.id')
class Meta:
model = User
fields = ('id',)
class SongSerializer(serializers.ModelSerializer):
performer = PerformerSerializer(many=True, read_only= True)
user= UserSerializer(many=False, read_only= True)
class Meta:
model = Song
fields = ('user','performer','id','name','song','song_image','likes','views')
read_only_fields = ('likes','views')
views.py
class SongCreateView(generics.ListCreateAPIView):
queryset = Song.objects.all()
serializer_class = SongSerializer
permission_classes = [IsAuthenticated]
Thank You.
I have a profile model which contains experience and education as foreign key fields.
When I access profile template, it throws.
I tried post_save,
def create_education(sender, instance, created, **kwargs):
if created:
Profile.objects.create(education=instance)
post_save.connect(create_education, sender=CustomUser)
it throws this error,
How do I define a post_save signal so experience and education are created when I create a profile?
Note: I double checked the error is because foreign fields are empty i.e there is no error when I add experience and education fields in django admin.
models.py
class Work_Experience(models.Model):
job_title = models.CharField(max_length=100, null=True, blank=True)
company = models.CharField(max_length=100, null=True, blank=True)
description = models.CharField(max_length=300, null=True, blank=True)
exp_start_date = models.DateField(null=True, blank=True)
exp_end_date = models.DateField(null=True, blank=True)
class Education(models.Model):
degree = models.CharField(max_length=100, null=True, blank=True)
school = models.CharField(max_length=100, null=True, blank=True)
edu_start_date = models.DateField(null=True, blank=True)
edu_end_date = models.DateField(null=True, blank=True)
class Profile(models.Model):
experience = models.ForeignKey(Work_Experience, on_delete=models.SET_NULL, null=True, blank=True)
education = models.ForeignKey(Education, on_delete=models.SET_NULL, null=True, blank=True)
forms.py
class ProfileSettingsForm(forms.ModelForm):
job_title = forms.CharField(max_length=40, required=False)
company = forms.CharField(max_length=40, required=False)
description = forms.CharField(max_length=40, required=False)
exp_start_date = forms.DateField(required=False)
exp_end_date = forms.DateField(required=False)
degree = forms.CharField(max_length=40, required=False)
school = forms.CharField(max_length=40, required=False)
edu_start_date = forms.DateField(required=False, input_formats=settings.DATE_INPUT_FORMATS)
edu_end_date = forms.DateField(required=False, input_formats=settings.DATE_INPUT_FORMATS)
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance', None)
super(ProfileSettingsForm, self).__init__(*args, **kwargs)
self.fields['job_title'].initial = self.instance.experience.job_title
self.fields['company'].initial = self.instance.experience.company
self.fields['description'].initial = self.instance.experience.description
self.fields['exp_start_date'].initial = self.instance.experience.exp_start_date
self.fields['exp_end_date'].initial = self.instance.experience.exp_end_date
self.fields['degree'].initial = self.instance.education.degree
self.fields['school'].initial = self.instance.education.school
self.fields['edu_start_date'].initial = self.instance.education.edu_start_date
self.fields['edu_end_date'].initial = self.instance.education.edu_end_date
def save(self, commit=True):
model = super(ProfileSettingsForm, self).save(commit=False)
jt = self.cleaned_data['job_title']
co = self.cleaned_data['company']
desc = self.cleaned_data['description']
esd = self.cleaned_data['exp_start_date']
eed = self.cleaned_data['exp_end_date']
degree = self.cleaned_data['degree']
school = self.cleaned_data['school']
edusd = self.cleaned_data['edu_start_date']
edued = self.cleaned_data['edu_end_date']
if model.experience:
model.experience.job_title = jt
model.experience.company = co
model.experience.description = desc
model.experience.exp_start_date = esd
model.experience.exp_end_date = eed
model.experience.save()
else:
model.experience = Work_Experience.objects.create(job_title=jt,
company=co,
description=desc,
exp_start_date=esd,
exp_end_date=eed)
if model.education:
model.education.degree = degree
model.education.school = school
model.education.edu_start_date = edusd
model.education.edu_end_date = edued
model.education.save()
else:
model.education = Education.objects.create(degree=degree,
school=school,
edu_start_date=edusd,
edu_end_date=edued)
if commit:
model.save()
return model
Views.py
class ProfileSettingsView(UpdateView):
model = Profile
form_class = ProfileSettingsForm
pk_url_kwarg = 'pk'
context_object_name = 'object'
template_name = 'profile_settings.html'
def get_success_url(self):
return reverse_lazy('users:profile_settings', args = (self.object.id,))
UPDATE
If I remove the init() method in form, the error resolves. But I don't get the values from database in the form fields once I save it.
How can I rewrite init() method?
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance', None)
super(ProfileSettingsForm, self).__init__(*args, **kwargs)
self.fields['job_title'].initial = self.instance.experience.job_title
self.fields['company'].initial = self.instance.experience.company
self.fields['description'].initial = self.instance.experience.description
self.fields['exp_start_date'].initial = self.instance.experience.exp_start_date
self.fields['exp_end_date'].initial = self.instance.experience.exp_end_date
self.fields['degree'].initial = self.instance.education.degree
self.fields['school'].initial = self.instance.education.school
self.fields['edu_start_date'].initial = self.instance.education.edu_start_date
self.fields['edu_end_date'].initial = self.instance.education.edu_end_date
Why the error?
In the line:
self.fields['job_title'].initial = self.instance.experience.job_title
you're dealing with a Profile instance that does not have a related experience.
How do I define a post_save signal so experience and education are created when I create a profile?
If you want every time you create a Profile it gets populated with am experience and education you should have a signal like:
def create_profile(sender, instance, created, **kwargs):
if created:
experience = Work_Experience.objects.create(profile=instance)
education = Education.objects.create(profile=instance)
post_save.connect(create_profile, sender=Profile)
Why the post_save signal is not triggered when calling the save() of the form?
model = super(ProfileSettingsForm, self).save(commit=False)
According the docs:
This save() method accepts an optional commit keyword argument, which accepts either True or False. If you call save() with commit=False, then it will return an object that hasn’t yet been saved to the database. In this case, it’s up to you to call save() on the resulting model instance. This is useful if you want to do custom processing on the object before saving it, or if you want to use one of the specialized model saving options. commit is True by default.
So by the time you do:
model.experience.job_title = jt
your post_save signal hasn't been triggered and therefore model.experience remains None hence the error:
'NoneType' object has no attribute job_title.
You cannot do that,i recommend you read django docs. just Do this:
Update here
The code bellow work as expected..
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.base_user import AbstractBaseUser
from latiro_app.managers import UserManager
class User(AbstractBaseUser):
email = models.CharField(verbose_name='email or phone number ', max_length=50, unique=True )
first_name = models.CharField('first name', max_length=15,blank=True)
last_name = models.CharField('last name', max_length=15,blank=True)
country = CountryField(blank=True)
date_joined = models.DateTimeField('date joined', auto_now_add=True)
slug = models.SlugField('slug', max_length=50, unique=True, null=True)
is_active = models.BooleanField('active',default=False)
is_staff = models.BooleanField('staff', default=False)
email_confirmed = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class Meta:
db_table = "users"
permissions = (
("edit_user", "Edit User"),
)
class WorkExperience(models.Model):
job_title = models.CharField(max_length=100, null=True, blank=True)
company = models.CharField(max_length=100, null=True, blank=True)
description = models.CharField(max_length=300, null=True, blank=True)
exp_start_date = models.DateField(null=True, blank=True)
exp_end_date = models.DateField(null=True, blank=True)
class Meta:
db_table = "experience"
def __str__(self):
return (self.job_title)
class Education(models.Model):
degree = models.CharField(max_length=100, null=True, blank=True)
school = models.CharField(max_length=100, null=True, blank=True)
edu_start_date = models.DateField(null=True, blank=True)
edu_end_date = models.DateField(null=True, blank=True)
class Meta:
db_table = "education"
def __str__(self):
return (self.degree)
class Profile (models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete= models.CASCADE,
verbose_name='list of users', null=True)
experience = models.ForeignKey(WorkExperience, on_delete=models.SET_NULL, null=True, blank=True)
education = models.ForeignKey(Education, on_delete=models.SET_NULL, null=True, blank=True)
def __str__(self):
return (self.user)
class Meta:
db_table = "profile"
#receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_profile(sender, instance, created, **kwargs):
if created and not kwargs.get('raw', False):
profile = Profile(user=instance)
profile.save()
This should work. Tested on my database:
+----+--------------+---------------+---------+
| id | education_id | experience_id | user_id |
+----+--------------+---------------+---------+
| 1 | NULL | NULL | 2 |
+----+--------------+---------------+---------+
The null values on education_id and experience_id will be update by user_id instance when updating profile.
Now User can update his/her profile like this:
note:i'm not using signal.
#Form.py
class EducationForm(forms.ModelForm):
degree = forms.CharField(max_length=40, required=False)
school = forms.CharField(max_length=40, required=False)
edu_start_date = forms.DateField(required=False,
input_formats=settings.DATE_INPUT_FORMATS)
edu_end_date = forms.DateField(required=False,
input_formats=settings.DATE_INPUT_FORMATS)
class Meta:
model= Education
fields =["degree","school", "edu_start_date","edu_start_date"]
#View.py
class EducationFormView(UpdateView):
model = Education
form_class = EducationForm
template_name = 'latiro_app/education_form.html'
def get_success_url(self):
return reverse_lazy('users:profile_settings',
args =(self.object.id,))
def get(self, form, ** kwargs):
profile_instance = get_object_or_404(self.model, user_id=self.kwargs['pk'])
#Pass user profile data to the form
context = {'form':self.form_class(instance=profile_instance)}
if self.request.is_ajax():
kwargs['ajax_form'] = render_to_string(self.template_name, context, request=self.request )
return JsonResponse(kwargs)
else:
return render(self.request, self.template_name, context)
I want to use hashing field set_password from User model in django.contrib.auth.models and I'm currently using a custom User model for that.
I'm getting the following error: Attribute error: 'User' object has no attribute 'set_password'
models.py
from django.db import models
class User(models.Model):
first_name = models.CharField(max_length=50, blank=True)
last_name = models.CharField(max_length=50, blank=True)
profile_picture =
models.ImageField(upload_to="user_data/profile_picture", blank=True)
username = models.CharField(max_length=100)
birth_date = models.DateField(blank=True)
gender = models.CharField(max_length=10, blank=True)
password = models.CharField(max_length=1000)
contact = models.CharField(max_length=10, blank=True)
email = models.CharField(max_length=100)
time_stamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.username
views.py
...
from .models import User
...
def post(self, request):
# Data is here
form = self.form_class(request.POST)
if form.is_valid():
# create object of form
user = form.save(commit=False)
# cleaned/normalised data
username = form.cleaned_data['username']
password = form.cleaned_data['password']
# convert plain password into hashed
user.set_password(user.password)
user.save()
return HttpResponse('Done here.')
...
forms.py (just used a widget in forms.py)
from .models import User
from django import forms
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'password']
This is a really easy fix. Just change your models.py file like so:
from django.contrib.auth.models import AbstractBaseUser
class User(AbstractBaseUser):
first_name = models.CharField(max_length=50, blank=True)
last_name = models.CharField(max_length=50, blank=True)
profile_picture = models.ImageField(upload_to="user_data/profile_picture", blank=True)
username = models.CharField(max_length=100)
birth_date = models.DateField(blank=True)
gender = models.CharField(max_length=10, blank=True)
password = models.CharField(max_length=1000)
contact = models.CharField(max_length=10, blank=True)
email = models.CharField(max_length=100)
time_stamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.username
That way, your user model will inherit all of the AbstractBaseUser methods, including set_password.
Look at this full example from the documentation for extra information.