Show related model info in Django form - python

Say, for example, I have two models:
class Person:
name = models.CharField(max_length=20)
address = models.CharField(max_length=20)
class PhoneNumber:
person = models.ForeignKey(Person)
number = models.CharField(max_length=20)
So, when a user is going to modify a PhoneNumber, I'd like to show, in the form, the address of the person. I'm doing the editing through formsets, so that makes it passing the information directly to the template a bit trickier, how could I know which address belongs to which form? Or how else could I achieve this? This should be shown in a different place than the ModelChoiceField, since I want it to be hidden.

Within your template is it possible to loop over the forms in a formset. See https://docs.djangoproject.com/en/1.7/topics/forms/formsets/#using-a-formset-in-views-and-templates.
So I would make a FormSet containing PhoneNumber modelforms. While looping over the forms, use form.instance.person.address.

Related

What fields can you call with request.user in Django?

Sorry for the dumb question but I can't figure it out.
I have a class based view for a form. I like to make some changes if the request.user is equal with something.
I used before in some other views request.user.profile.leader that gives me a boolean answer. Thats OK.
Now in this class based view I like to use a very similar stuff but with another model like this: request.user.trust.group but it gives me nothing. What am I doing wrong?
If you haven't customized your user model, then profile will appear on it as a reverse descriptor of an one-to-one field on another model (by way of related_name having been set or inferred), i.e. you have something like
class Profile(Model):
user = models.OneToOneField(User, related_name="profile")
leader = models.BooleanField(...)
somewhere.
If you expect a trust field to be there, then you'd need something similar:
class Trust(Model):
user = models.OneToOneField(User, related_name="trust")
group = ...
On the other hand, if you do have an entirely custom user model, then those attributes could appear directly on it:
class CustomUser(AbstractBaseUser):
profile = models.ForeignKey(Profile, ...)
trust = models.ForeignKey(Trust, ...)

Multiple User type with Django

Using Django we have two types of Users (Teachers and Students) with common fields and uncommon fields
In our wizard we first POST the common fields to /users with an extra type_field
Every operation after this should be able to figure out which model (Teacher or Student) it needs to use.
We are thinking of making two models ( Teacher and Student ) with an one-to-one field.
But how do we hookup the type_field to the right Model on every operation?
You dont have to go for an extra field since you are already having two different classes for students and teachers. A simple approach may looks like below.
from django.contrib.auth.models import User
class Teacher(User):
extra_field_1 = models.Fieldtype()
...
...
class Student(User):
extra_field_1 = models.Fieldtype()
...
...
You can provide both type of users same registration form and upon clicking next take them to next page based on the value of I am a teacher/student field. In that case I suggest you to use atomic blocks if you dont want to save data in case registration procedure fails at some point or user have selected a wrong choice and they want to go back. By this approach each inherited models have username, first_name, last_name and email that you dont have to insert any of these to Teacher or student model.
Then you have to create forms for each model. You may use modelform A much better approach will be using class based views since that reduce a lot of code and stick to dry principles.
You may use something like:
class Person(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
# more common fields
class Student(Person):
specific_field_to_student = models.CharField(max_length=255)
# more fields
class Teacher(Person):
specific_field_to_teacher = models.CharField(max_length=255)
# more fields
In your database you will have 3 tables (yourapp_person, yourapp_student and yourapp_teacher). Now, if type_field value is student, you will use Student model to create user, if it is teacher, you will use Teacher model.
Note: You may need to make Person model above a subclass of the built-in User model.
Edit:
I have edited the model above to take into account the requirements in the comments below.
Now, to retrieve user by id, you can use the following code in your view:
user = Person.objects.get(id=id) # id is the view param
if hasattr(user, 'student'):
print("Student")
else: # hasattr(user, 'teacher')
print("Teacher")

Large ManyToMany relations on django admin form

So, I have the following models:
class Band(models.Model):
name = models.CharField(max_length=50)
class Contract(models.Model):
band = models.ForeignKey(Band)
when = models.DateTimeField(auto_now_add=True)
salary = models.IntegerField()
class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
instrument = models.CharField(max_length=100)
bands = models.ManyToManyField(Band, through=Contract)
class Album(models.Model):
artist = models.ForeignKey(Musician)
name = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()
So, I wanted to expose that on the admin page. So far, so good.
Note that our musicians here keep jumping in and out from bands. Some say one of them even had been on over 2 millions bands in his life-time. I don't know, maybe the bands are Whitesnake, Metallica or something.
How should we do that on the Django Admin Page?
I tried using raw_id_fields and apart the fact I didn't like the effect, it didn't work so well. It took a lot of time to load and it didn't let me add more ids. Weird.
I've used admin.StackedInline with no luck cause it will try to load every contract in a which, well, it's gonna take only 2 thousand years.
When Musician had a direct relation to Band it worked just fine with this library. But now that the relation isn't an straight one. Looks like autocomplete doesn't support it(it was getting slow anyway).
So, with all of this, I ask you lord SO members. What's the best way to do this? Is it autocomplete? Someone must have had to come across this issue!
Thanks in advance.
To avoid loading every bands in your admin page use autocomplete_fields Django doc.
Just use it like that in your admin.py.
autocomplete_fields = ('bands',)
Then no bands will be pulled from DB to front, but you will be able to select it through a Select2 search field and it will be printed as "tags".
I found this solution and hope it will help somebody in the same situation:
I have many to many relations between the Product and Characteristic model.
So, in the admin.py I am setting a form for a Product like the following where catch/get all the Characteristics and make the "prefecth_related" for Characteristic, as well the "select_related" could be done there:
class ProductAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['characteristics'].queryset = Characteristic.objects.prefetch_related('category').all()

Need help on Django Modelling

I am creating a Django app and this questions comes to hit me again (I've already run into it but I resolved it in a way I don't think is correct).
So I have these models:
def Person(models.Model):
name = models.CharField(max_length=30)
status = models.ManyToManyField(Status)
def Status(models.Model):
name = models.CharField(max_length=30)
Any person may have up to 3 status (or none). And this was supposed to be a string array containing only a few possible status. e.g. happy, lucky, strong.
However a CharField with choices would not be possible here, as one person may feel happy and strong at the same time.
Is my current approach the best one possible?
You will need to validate the statuses count for each person in the clean method.
from django.db import models
from django.core.exceptions import ValidationError
class Status(models.Model):
name = models.CharField(max_length=30)
class Person(models.Model):
name = models.CharField(max_length=30)
status = models.ManyToManyField(Status)
def clean(self):
# check person status count
if self.status.all().count() > 3:
raise ValidationError("Person can not have more than 3 statuses")
Update
Since it's a ManyToMany relationship, you will never get this validated during the creation of the object, unless you have a separate form for adding statuses for persons.
If you have statuses field in the same form of person creation, then this check MUST be in the form.
class PersonForm(forms.ModelForm):
class Meta:
model = Person
def clean(self):
statuses = self.cleaned_data.get('status')
if status.count() > 3:
raise ValidationError("Person can not have more than 3 statuses")
Why this method
This design of models will allow you to have many different ways of querying. such as get people who are happy! or count of people who are sad and finally get people with similar statuses
Just a quick addition - if you're using PostgreSQL (and that's probably the best decision) you could also use an Array Field, for which you can specify a max size.
https://docs.djangoproject.com/en/1.8/ref/contrib/postgres/fields/
Another option is to use a tagging library like Django-taggit

django: need to design models/forms for a 'multiple level nested' structures

Assume some Company with Employees. There are Name and Contact information bound to each Employee. Each Contact contains Street and Phones fields.
What I want is a page which lists employees within a company. But everything must be listed as forms. Because I want to be able to modify the particular Employee information and the most important - I want to be able to add new Employees (clicking a button "Add new employee" must add a new empty "Employee form"). As well as it must allow to add a new phone number to the existing Employee's Contact information any time.
The data model looks like:
--Company
----Employee1
------Name
------Contact
--------Street
--------Phones
----------Phone1
----------Phone2
----Employee2
------Name
------Contact
--------Street
--------Phones
----------Phone1
----------Phone2
----------Phone3
...
Could someone please help to design Models and Forms for such a task? Your help is very much appreciated. Many thanks!
P.S. Forgot to mention that I want all the data "collected" in the Company object at the end of the day. I mean when I serialize c = Comapany.objects.all()[0] on the back end the entire employee information must be visible, like c.employees[0].contact.phones[0] must be the first employee's first phone number. Thanks.
P.P.S.
That is not the case that I'm just forwarding my project. This is just an hypothetical example I'd created to present the problem. I'm a django newbie and trying to understand how the framework gets things rolling.
I've spent lot of time on this. I've found several ways to go, but no one got me to the end. For instance, a wonderful blog about nested formsets http://yergler.net/blog/2013/09/03/nested-formsets-redux/ helped with forms and rendering. But, it solved only the half of the problem. The data like I mentioned above is not "being collected" into an object. At the end of the day I want to serialize a Company object and save it in yaml format using pyyaml (see my previous post django: want to have a form for dynamically changed sequence data).
Django is perfect with "static" models and forms, ModelForms are awesome. But what if your model needs to be changed dynamically? No standard way to go. Either no appropriate documentation nor I could find a one. Thus, I'd like to hear how experts imagine the solution for such a problem.
Try this:
from django.db import models
class _Contact(object):
pass
class Company(models.Model):
name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
#property
def employees(self):
return self.employee_set.prefetch_related('phones').order_by('-created_at')
class Phone(models.Model):
number = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
class Employee(models.Model):
name = models.CharField(max_length=255)
street = models.CharField(max_length=255)
phones = models.ManyToManyField('Phone', through='EmployeePhone', blank=True)
created_at = models.DateTimeField(auto_now_add=True)
company = models.ForeignKey(Company)
#property
def contact(self):
_contact = _Contact()
_contact.street = self.street
_contact.phones = self.phones.order_by('-employeephone__created_at')
return _contact
class EmployeePhone(models.Model):
employee = models.ForeignKey(Employee)
phone = models.ForeignKey(Phone)
created_at = models.DateTimeField(auto_now_add=True)
However, you should just use employee.street and employee.phones. employee.contact is redundant.

Categories