Class based views how to handle OneToOneField in a model - django

I have 2 models:
class CompanyInfo(models.Model):
name = models.CharField(_('name'), max_length=100)
address = models.OneToOneField(Location)
class Location(models.Model):
address_1 = models.CharField(_("address"), max_length=128)
address_2 = models.CharField(_("address cont'd"), max_length=128, blank=True)
city = models.CharField(_("city"), max_length=64, default="")
state = USStateField(_("state"), default="")
zip_code = models.CharField(_("zip code"), max_length=5, default="")
When I use CBVs and prints out the form on the template. It shows name with an input field and address as a multiple choice selection.
Is there anyway in which I could convert the multiple choice to act as multiple input fields and the state to be multiple choice.
I figured I could have 2 form for these, but how would I incorporate a form inside of a form?
Also, the way in which the models and fields are must not be changed. For this example sure location fields could be in the company info, but I simply want how to do something similar when it doesn't make sense to include all these fields into the same model.
So, far only thing I can come up with is using function based views and just dealing with 2 forms at a time.
Here are my forms:
class CompanyInfoForm(ModelForm):
class Meta:
model = CompanyInfo
exclude = ['address']
class LocationForm(ModelForm):
class Meta:
model = Location
fields = '__all__'
then class based views:
class CompanyInfoCreate(CreateView):
model = CompanyInfo
form_class = CompanyInfoForm
class LocationCreate(CreateView):
model = Location
form_class = LocationForm
However, this is very helpful since these forms can only be done 1 at a time. I would like LocationView to be in place of the address location or of the sort.
Perhaps, these types of views have their strength in dealing with forms at an individual level.

Related

How to use a serializer for different fields

I am working on a project of blog application in Django Rest Framework. But here I am facing some trouble. At first checkout my code then I will explain the question.
Here is the model.py
class Contact(models.Model):
id_no = models.AutoField(primary_key=True, unique=True)
email = models.EmailField()
name = models.CharField(max_length=1000)
subject = models.CharField(max_length=1000)
description = models.TextField()
And here is the serializer.py
class AddContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields = '__all__'
Now in a view function I want to use only email and name field of the Contact model and in another view function I want to use name and description field of that model.
Can I use the same serializer class for different cases?
Please help me.
You can create multiple seralizers for a model. Example
class EmailContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields = ['email','name'] # your desired fields here

Django forms how to initialze only not required field instead of passing all required fields

class Student(models.Model):
name = models.CharField(null=True, blank=True)
fieldA = models.TextField(null=True, blank=True)
fieldB = models.TextField(null=True, blank=True)
fieldC = models.TextField(null=True, blank=True)
fieldD = models.TextField(null=True, blank=True)
class Meta:
model = Student
fields = '__all__'
Based on the above example all fields are taken in UpdateView or CreateView.
If i need to take only required field i can change Meta to
class Meta:
model = Student
fields = ['name','fieldA',fieldB','fieldC']
So my question is how can add only fields that should not pass in the Meta.From above example 'fieldD' is not passed. Is there is any way to say 'fieldD' is not required instead on passing all required fields in Meta.So that the code can be reduced.
Above one is a small example. Consider I have 200 fields and only 1 field that I didnot want to pass so instead of passing 199 required fields in Meta is there is anyway to tell only the 1 field should not pass
Use Meta.exclude instead of Meta.fields to define only the fields that should not be present on the form
class StudentForm(forms.ModelForm):
class Meta:
model = Student
exclude = ['fieldD']

Get the object name in django admin

Hej!
I want to have a category for turnovers in my admin area and in there I have a field with currency. It is a dropdown field with pre given currencies.
My problem is, that there aren't the actual currencies shown rather their IDs. (like Object 123).
Does anyone know how to solve this for an inline?
# models.py
class Currency(models.Model):
code = models.CharField(max_length=3, unique=True)
currency = models.CharField(max_length=150, unique=True)
class Turnover(models.Model):
currency = models.ForeignKey(Currency, on_delete=models.PROTECT)
# admin.py
class TurnoverInline(admin.TabularInline):
model = Turnover
extra = 1
classes = ["collapse"]
class SomeAdmin(admin.ModelAdmin):
form = SomeForm
save_on_top = True
inlines = [
TurnoverInline
]
For a similar problem I created admin.ModelAdmins and integrated a code_str(self) and str(self) method in the model. But those aren't Inlines and in the SomeForm/SomeAdmin as fields/autocomplete_fields.
You can use __str__ in the Currency model:
class Currency(models.Model):
code = models.CharField(max_length=3, unique=True)
currency = models.CharField(max_length=150, unique=True)
def __str__(self):
return f'{self.code} - {self.currency}'
The dropdown should then show this format instead of the object and id.

Django ManyToMany Validation Constraint

I have a ManyToMany link, and a Foreign key which links three objects.
[A]>--<[B]>---[C]
A can belong to many of B, and vice versa. However, A can only belong to B objects with the same parent C.
I'm trying to do something in the clean() method of the model. I'm using Django Rest Framework and no ModelForms or anything like that. I haven't been able to figure it out yet
Simplified Sample Code
class Device(models.Model):
name = models.CharField(max_length=20)
projects = models.ManyToManyField(Project, 'devices')
details = models.CharField(max_length=200)
serial = models.CharField(max_length=20)
address models.GenericIPAddressField(default="0.0.0.0")
port = models.IntegerField(default=3000)
jumpers = models.IntegerField(default=0)
install_date = models.DateField(blank=True, null=True)
class Project(models.Model):
name = models.CharField(max_length=20)
description = models.CharField(max_length=250)
area = models.ForeignKey(Area)
class Area(models.Model):
name = models.CharField(max_length=20)
description = models.CharField(max_length=250)
owner = models.CharField(max_length=20) # microservice doesn't have owner group - field in JWT
Serializers
class AreaSerializer(serializers.ModelSerializer):
class Meta:
model = Area
fields = ('name', 'description', 'owner')
class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = ('id', 'name', 'description', 'area')
class DeviceSerializer(serializers.ModelSerializer):
class Meta:
model = Device
fields = ('id', 'name', 'projects', 'details', 'serial',
'address', 'port', 'jumpers', 'install_date')
I am not sure where and how do you want to validate your data. So I am just posting the method which can validate if a project can be linked to a device or not based on your specific check.
def validate_project(device, project):
projects = device.projects.all()
areas = set(projects.values_list('area', flat=True))
if len(areas) > 1:
raise serializers.ValidationError('projects are not valid')
return areas.pop() == project.area_id
EDIT:
You have to use a intermediate model for storing the relationship between device and project.
class Membership(models.Model):
device = models.ForeignKey(Device, on_delete=models.CASCADE)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
area = models.ForeignKey(Area, on_delete=models.CASCADE)
use the above membership model to store the many to many relations.
On your device model use this field to define the many to many relation.
projects = models.ManyToManyField(Project, through='Membership')
checkout the docs
Now when you link a device and project you will have explicitly add the area id as well. Before adding now you can check if the project is valid or not based on the area associated.
(ignore the wonky field types, cba)
What it boils down to is: you need a table BC that stores relations between B and C. Table A would then select only from those relations through the intermediary m2m table ABC (or ditch ABC, couldn't figure out how to draw m2m with the online tool). I think I mixed up B and C in this picture, swap them around depending on whether B or C holds the ForeignKey.
Please correct if I'm wrong!

form with manytomany object creation

I'm trying to make own form adding object Announcement
models.py:
class Announcement(models.Model):
person = models.ForeignKey('Person')
source = models.CharField(max_length=30)
date = models.DateField(default=datetime.now())
valid_date = models.DateField(null=True,blank=True)
class Person(models.Model):
names = models.ManyToManyField('Name')
birth_date = models.DateField(null=True, blank=True)
class Name(models.Model):
first_name = models.CharField(max_length=50,blank=True)
middle_name = models.CharField(max_length=50, blank=True)
last_name = models.CharField(max_length=50, blank=True)
(It maybe loos weird, but in my concept each Person can have more than one Name, and also the same Name can be assigned to different Persons)
forms.py
from django import forms
from backoffice.models import Announcement
class AnnouncementForm(forms.ModelForm):
class Meta:
model = Announcement
fields = ('person','signature','organisation','source', 'date')
And everything works perfectly but I have to choose Person from selectbox. And it is expected behaviour.
But in my case i'm definetely sure, that person doesn't exists in base (all announcements are for different person for very long time - so i want to change person select box to three fields, and create new person (with new names) everytime I save the announcement.
I think I know how to save many to many, that's why i don't put the views.py, but I don't know how to set the forms.py to get fields.
I tried
class AnnouncementForm(forms.ModelForm):
class Meta:
model = Announcement
fields = ('person__names__first_name','signature','organisation','source', 'date')
but got Unknown field(s) (person__names__first_name) specified for Announcement
person__name__first_name will not really work in the forms, that only works for the django admin
you have to create a custom field for the first name and then create a logic for saving on either
def clean(self):
// logic here
or
def save(self, commit=True):
// put clean data here
announcement_form = super(AnnouncementForm, self).save(commit=False)
announcement_form.save()