Django ModelForm - form display values - django

I have a model:
class SchedulerProfile(models.Model):
user = models.ForeignKey(User, unique=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
def __unicode__(self):
return u'%s' % (self.user)
I also have a model that has a M2M relationship with the Scheduler:
class OfficialProfile(models.Model):
user = models.ForeignKey(User, unique=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
schedulers = models.ManyToManyField(SchedulerProfile, verbose_name="Your schedulers", related_name="off_schedulers")
When I create a ModelForm from the OfficialProfile, the form renders the contents of the scheduler multiselect as the username. I'd like to get it to display user.first_name user.last_name in the field instead of the user. I know I can change the unicode return value to accomplish this, but I'd rather not do that for reasons. In the __init__ of the ModelForm, you should be able to do something like:
self.fields['schedulers'].queryset = SchedulerProfile.objects.values('user__first_name', 'user__last_name')
But that doesn't produce the desired results. This seems like a really basic question, but searching for the answer hasn't revealed much.

Subclass ModelMultipleChoiceField and override label_from_instance.
from django.forms import ModelMultipleChoiceField
class SchedulerProfileChoiceField(ModelMultipleChoiceField):
def label_from_instance(self, obj):
return "%s %s" % (obj.first_name, obj.last_name)
Then use your multiple choice field in the model form.
class OfficialProfileForm(forms.ModelForm):
schedulers = SchedulerProfileChoiceField(queryset=SchedulerProfile.objects.all())

Related

Try to join a OneToOne relationship in Django

I need some help doing a join using Django, which seems like it should be easy. I have looked at the documentation but it seems like it won't join for some reason.
I am trying to get in my view, the model.Photo and model.PhotoExtended with both joined and then displayed in the view. Currently I am just trying to get the model.Photo displayed but with a join which finds the request.user and filters it based on that.
They are in different apps.
models.py for model.Photo
class Photo(ImageModel):
title = models.CharField(_('title'),
max_length=60,
unique=True)
slug = models.SlugField(_('slug'),
unique=True,
help_text=_('A "slug" is a unique URL-friendly title for an object.'))
models.py for model.PhotoExtended
class PhotoExtended(models.Model):
Photo = models.OneToOneField(Photo, related_name='extended', help_text='Photo required', null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, help_text='User that uploaded the photo')
views.py
class PhotoExtendedUserView(ListView):
template_name = 'photo_user_list.html'
def get_queryset(self):
user = get_object_or_404(User, username=self.request.user)
return Photo.objects.filter(photoextended__user=user)
You set the related_name on Photo (which shouldn't be capitalized by the way) to extended so you need to filter like so:
class PhotoExtendedUserView(ListView):
template_name = 'photo_user_list.html'
def get_queryset(self):
user = get_object_or_404(User, username=self.request.user)
# 'extended' vs. 'photoextended'
return Photo.objects.filter(extended__user=user)

Display full names in Form ChoiceField but saving ID's

I have model Person - from another database
I copied all person_id to custom_id.
models.py
class Employee(models.Model):
custom_id = models.CharField(max_length=20, unique=True)
#property
def person(self):
return Person.objects.get(person_id='%s' % self.custom_id)
def __str__(self):
return '%s' % self.custom_id
class Task(models.Model):
employee = models.ManyToManyField(Employee, blank=True, null=True)
task = models.CharField(max_length=100)
comment = models.CharField(max_length=200)
def __str__(self):
return '%s' % self.task
I add my method person() to Employee which allow me to access other objects model in another database:
So basically when I type this in shell:
Employee.objects.get(custom_id='123').person.full_name
u'Adam Dylan'
I have a ModelForm which use ModelMultipleChoiceField
forms.py
class TaskCreateForm(forms.ModelForm):
employee = forms.ModelMultipleChoiceField(queryset=Employee.objects.all())
class Meta:
model = Task
But Employee.objects.all() returns bunch of custom_id's.
What I want is to show in form "Employee(..).person.full_name" but saving only custom_id's.
I am not sure why you think the answer I gave to your other question does not work here. Did you try the following? If it does not work, how exactly does it fail?
class EmployeeMultipleChoiceField(ModelMultipleChoiceField):
def label_from_instance(self, obj):
return obj.person.full_name
class TaskCreateForm(forms.ModelForm):
employee = EmployeeMultipleChoiceField(queryset=Employee.objects.all())
class Meta:
model = Task

Django createview using primary key

I have a model like this:
class Appointment(models.Model):
user = models.ForeignKey(User)
engineer = models.ForeignKey(Engineer)
first_name = models.CharField(max_length=100)
middle_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100)
age = models.IntegerField()
def __unicode__(self):
return self.first_name
I have created a form using this model and I want to save it. Here I want the 'engineer' field to to be as the primary key is passed in the url.. like
engineer = Engineer.objects.get(pk=pk)
How can I do this. Or I should create a normal form and get its value via get method and assign to the field??
Since this is a CreateView, you should build on the example in the documentation and add it in form_valid:
def form_valid(self, form):
form.instance.engineer = Engineer.objects.get(pk=self.kwargs['pk'])
return super(AppointmentCreate, self).form_valid(form)

display foreignkey field in django template

I have models.py like below:
class Profile(models.Model):
user = models.OneToOneField(User)
def __unicode__(self):
return "%s" % self.user
class Student(models.Model):
user = models.ForeignKey(Profile)
phone_num = models.CharField(max_length=15)
def __unicode__(self):
return "%s" % (self.user)
class Teacher(models.Model):
user = models.ForeignKey(Profile)
phone_num = models.CharField(max_length=15)
def __unicode__(self):
return "%s" % (self.user)
class Complaint(models.Model):
user = models.ForeignKey(Student)
complaint = models.TextField()
teacher = models.ForeignKey(Teacher, null=True)
def __unicode__(self):
return "%s : %s" % (self.complaint)
How can I display teacher's name which is eventually stored in class Profile
What I get is a column teacher_id in _complaint table when I do
student_obj = Student.objects.get(name=user_profile_instance)
and then
compaint = student_obj.complaint_set.values()
in complaint.html
{{complaint.teacher_id}}
what I want is teacher name instead of id
This should work -
{{ complaint.teacher.user.user.first_name }}
First of all Please Update your style of coding to make your App Optimised
Use
student_obj = Student.objects.select_related().get(name=user_profile_instance)
The Above one will Cache Datas . After that each time when u call Datas from fields it Wont HIT your database :) , Hence Your App will Fly
instead of
student_obj = Student.objects.get(name=user_profile_instance)
and i'm Agreeing with #Bibhas Answer
{{ complaint.teacher.user.user.first_name }}
Teacher's Profile is Inheriting Django's Auth Model User
That wise
user.user.first_name

How do I reference Django Model from another model

Im looking to create a view in the admin panel for a test program which logs Books, publishers and authors (as on djangoproject.com)
I have the following two models defined.
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField()
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __unicode__(self):
return self.title
What I want to do, is change the Book model to reference the first_name of any authors and show this using admin.AdminModels.
#Here is the admin model I've created.
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'publisher', 'publication_date') # Author would in here
list_filter = ('publication_date',)
date_hierarchy = 'publication_date'
ordering = ('-publication_date',)
fields = ('title', 'authors', 'publisher', 'publication_date')
filter_horizontal = ('authors',)
raw_id_fields = ('publisher',)
As I understand it, you cannot have two ForeignKeys in the same model. Can anyone give me an example of how to do this?
I've tried loads of different things and its been driving me mad all day. Im pretty new to Python/Django.
Just to be clear - I'd simply like the Author(s) First/Last name to appear alongside the book title and publisher name.
Thanks
You can have more than one Foreign Key on a model.
If you would put a Foreign-Key field's name in list_display you will always just see the __unicode__ representation of the associated model.
But you can add a function like this to your BookAdmin:
def first_names(self, obj):
return ','.join(a.first_name for a in obj.authors.all())
get_sites.short_description = 'First Names'
Then add 'first_names' to list_display.