I wanna overrite the admin page - django

In the admin page when I create a property first I want to have a dropdown when I can choose the city and then another dropdown when I can choose a district where there are Districts that have ForeignKey key the city I selected
class City(models.Model):
name = models.CharField(verbose_name="Emri", help_text="E nevojshme", max_length=255, unique=True)
...
class District(models.Model):
city_id = models.ForeignKey(City, on_delete=models.CASCADE)
name = models.CharField(verbose_name="Emri", help_text="E nevojshme", max_length=255, unique=True)
...
class Property(models.Model):
district_id = models.ForeignKey(District, on_delete=models.CASCADE)
...

Related

Is it good database design?

I have this task to create api:
City district - name, id
Category - name, id
Organization Network - name, id
Organization
Belongs to one of the organization networks
id, name, description
belongs to several districts, can be represented in several of them at the same time
have list of products with prices
Product
id, name, category
can be sold in one or several organizations in network
price can be different depending on the organization
This is my try to design this database, am I missing something?
And also in Django:
from django.db import models
class District(models.Model):
name = models.CharField(max_length=100, unique=True)
class Category(models.Model):
name = models.CharField(max_length=100, unique=True)
class OrganizationNetwork(models.Model):
name = models.CharField(max_length=100, unique=True)
class Organization(models.Model):
name = models.CharField(max_length=100, unique=True)
description = models.TextField()
organization_network = models.ForeignKey(
OrganizationNetwork,
on_delete=models.CASCADE,
related_name="organizations",
)
district = models.ManyToManyField(
"District",
through="DistrictOrganization",
through_fields=("organization", "district"),
related_name="organizations",
)
products = models.ManyToManyField(
"Product",
through="OrganizationProduct",
through_fields=("organization", "product"),
related_name="organizations",
)
class Product(models.Model):
name = models.CharField(max_length=100, unique=True)
category = models.ForeignKey(
Category, on_delete=models.CASCADE, related_name="products"
)
class DistrictOrganization(models.Model):
district = models.ForeignKey(District, on_delete=models.CASCADE)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
class OrganizationProduct(models.Model):
price = models.DecimalField(max_digits=10, decimal_places=2)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
It`s pretty strange to create 2 relations between organization and district. You should use one to many if there could be only one organization in district, many to many otherwise.

Add Depandent drop down list for Django admin user

I have created 4 models in my django Country, State, and City, and also add them in admin.site.register How Can I add dependent drop down list for Country State City for admin user whenever user try to create Aplications object, they get state name list depends on Country name selected by admin user, and also for city.
Models.py
from django.db import models
from django.db.models import ForeignKey
from multiselectfield import MultiSelectField
class Country(models.Model):
name = models.CharField(max_length=250)
phone_code = models.CharField(max_length=250)
currency = models.CharField(max_length=250)
def __str__(self):
return self.name
class State(models.Model):
name = models.CharField(max_length=250)
country = models.ForeignKey(to=Country, on_delete=models.CASCADE)
def __str__(self):
return self.name
class City(models.Model):
state = models.ForeignKey(to=State, on_delete=models.CASCADE)
name = models.CharField(max_length=250)
def __str__(self):
return self.name
class Applications(models.Model):
country = models.ForeignKey(Country, on_delete=models.SET_NULL, null=True)
state = models.ForeignKey(State, on_delete=models.SET_NULL, null=True)
city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True)
name = models.CharField(max_length=20)
phone_number = models.IntegerField()
email_id = models.EmailField()
home_address = models.CharField(max_length=255, blank=True, null=True)
birthdate = models.DateField(null=True, blank=True)
current_company = models.TextField(max_length=250, blank=True, null=True)
def __str__(self):
return str(self.name)
they get state name list depends on Country name selected by admin user, and also for city
Since you said a dropdown list, I will suggest switching over to a multiple choice field where the choices will be set to a certain range of values and will appear as a dropdown in the admin dashboard.

Filter Album with empty set of tracks Django-Model

I have two models:
class Album(models.Model):
album_name = models.CharField(max_length=100)
artist = models.CharField(max_length=100)
class Track(models.Model):
album = models.ForeignKey(
Album, related_name='tracks',
on_delete=models.CASCADE,
null=True
)
order = models.IntegerField()
title = models.CharField(max_length=100)
duration = models.IntegerField()
Now how can i get the collection of Album with empty set of tracks?
Try to use isnull:
Album.objects.filter(tracks__isnull=True)
Just read the doc here. You can't save a model with field who haven't value or you need to had null=True to allow null field save in your database
models.py
class Album(models.Model):
album_name = models.CharField(max_length=100, null=True)
artist = models.CharField(max_length=100, null=True)
class Track(models.Model):
album = models.ForeignKey(Album, related_name='tracks', on_delete=models.CASCADE, null=True)
order = models.IntegerField(null=True)
title = models.CharField(max_length=100, null=True)
duration = models.IntegerField(null=True)
Info: if your database is Postgre, it's recommended to initialize field at null=True.
EDIT: thank so much for people who had negative point but never give answer.
To get list of Album with empty tracks. You need to save empty tracks first, related to the album (it's why you need to add null=True) and do a query like that :
Album.tracks.all()
This query take the Album of your choice, and select all tracks who have this album was related name. So the result is a query with the list of tracks who have Album has key.

Django inheritence question

some body please explain me the following i have two classes Userprofile and Staff.staff inherits userprofile
My Question is that if any entry has to be made to a staff table .1.It would be mandatory to fill out the details of user profile right?
Please explain this inheritence.Thanks
class Profile(models.Model):
user = models.ForeignKey(User, unique=True)
created_date = models.DateTimeField(auto_now_add=True)
emp_first_name = models.CharField(max_length=255)
emp_last_name = models.CharField(max_length=255)
gender = models.CharField(max_length=2, choices = GENDER_CHOICES, null=False)
date_of_birth = models.DateField(blank=True,null=True)
address1 = models.CharField(max_length=255)
city = models.CharField(max_length=48)
state = models.CharField(max_length=48)
country = models.CharField(max_length=48)
email_id = models.EmailField(blank=True)
class Staff(UserProfile):
role = models.ManyToManyField(Role)
designation = models.CharField(max_length=48, blank=True, null=True)
education = models.CharField(max_length=255, blank=True, null=True)
If you take a look at https://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance you'll see that automatic One-to-One mappings are created. Therefore, an entry for UserProfile is saved, and an entry for Staff is saved with a OneToOne field that points to the UserProfile entry.
However, if you want Staff to just inherit all the fields, I'd recommend setting abstract = True in your UserProfile. This means that Staff inherits all those fields, but you can never create a UserProfile by itself (as described at
https://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes )
class Meta:
abstract = True

Get a dropdown of states in Django Admin

I made some basic models for a listing of a business, like so:
class Business(models.Models):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=225, blank=True)
address = models.CharField(max_length=150, blank=True)
city = models.CharField(max_length=150, blank=True)
state_id = models.IntegerField(null=True, blank=True)
zip = models.CharField(max_length=33, blank=True)
country = models.CharField(max_length=150, blank=True)
url = models.CharField(max_length=765, blank=True)
class States(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=96)
state_abbr = models.CharField(max_length=24, blank=True)
In the admin when I edit each business it shows the state_id field. But how do I connect it with the state model to show a select dropdown listing of the states?
Also, how do I show the state abbreviation in the view of a business?
An alternative that doesn't require a separate state table:
from django.contrib.localflavor.us.us_states import STATE_CHOICES
class Business(models.Models):
...
state = models.CharField(max_length=2, choices=STATE_CHOICES, null=True, blank=True)
...
Edit in 2015 (django 1.8)
you should check the django official localflavor repo: https://github.com/django/django-localflavor.
from localflavor.us.models import USStateField
class Business(models.Models):
…
state = USStateField(null=True, blank=True)
…
Some tests are available on the repo for this specific usage.
Docs available here.
You need to use a ForeignKey field.
Make the following changes.
class Business(models.Models):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=225, blank=True)
address = models.CharField(max_length=150, blank=True)
city = models.CharField(max_length=150, blank=True)
#state_id = models.IntegerField(null=True, blank=True)
# Define a new state field that creates a ForeignKey relationship with States
state = models.ForeignKey('States', null=True, blank=True)
zip = models.CharField(max_length=33, blank=True)
country = models.CharField(max_length=150, blank=True)
url = models.CharField(max_length=765, blank=True)
class States(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=96)
state_abbr = models.CharField(max_length=24, blank=True)
#Define the __unicode__ method, which is used by related models by default.
def __unicode__(self):
return self.state_abbr
By default ForeignKey fields append '_id' to the field name when creating the column name in the database. So, the new "state" field in the Business class will automatically use the column "state_id" that you've previously defined, unless you've changed some of the default behavior of Django.
For more on this:
Check out Django's documentation of
the ForeignKey field
Search "ForeignKey" on stackoverflow.com