Add City & Location models
This commit is contained in:
parent
ae458acc77
commit
80cf963b7f
3 changed files with 86 additions and 0 deletions
|
@ -1,3 +1,27 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
|
from gallery.models import City, Location
|
||||||
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class CityAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('name',)
|
||||||
|
search_fields = ('name',)
|
||||||
|
ordering = ('name',)
|
||||||
|
list_per_page = 30
|
||||||
|
|
||||||
|
|
||||||
|
admin.site.register(City, CityAdmin)
|
||||||
|
|
||||||
|
|
||||||
|
class LocationAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('city', 'place')
|
||||||
|
list_filter = ('city',)
|
||||||
|
ordering = ('city__name',)
|
||||||
|
search_fields = ('place', 'city__name',)
|
||||||
|
list_per_page = 30
|
||||||
|
|
||||||
|
|
||||||
|
admin.site.register(Location, LocationAdmin)
|
||||||
|
|
37
gallery/migrations/0001_initial.py
Normal file
37
gallery/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
# Generated by Django 5.1.4 on 2024-12-29 13:38
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='City',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('name', models.CharField(max_length=150, unique=True, verbose_name='City')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'verbose_name_plural': 'Cities',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Location',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('place', models.CharField(blank=True, max_length=250, verbose_name='Place')),
|
||||||
|
('city', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='places', to='gallery.city', verbose_name='City')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'verbose_name_plural': 'Locations',
|
||||||
|
'unique_together': {('place', 'city')},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,3 +1,28 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
||||||
|
class City(models.Model):
|
||||||
|
name = models.CharField(max_length=150, unique=True, verbose_name="City")
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name_plural = "Cities"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return '{}'.format(self.name)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Location(models.Model):
|
||||||
|
place = models.CharField(max_length=250, blank=True, null=False, verbose_name="Place")
|
||||||
|
city = models.ForeignKey(City, on_delete=models.CASCADE, related_name='places', verbose_name="City")
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name_plural = "Locations"
|
||||||
|
unique_together = ('place', "city")
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
if self.place:
|
||||||
|
return '{0}, {1}'.format(self.city, self.place)
|
||||||
|
else:
|
||||||
|
return '{}'.format(self.city)
|
||||||
|
|
Loading…
Reference in a new issue