Фикс связей таблицы с друзьями

main
through-your-tears 9 months ago
parent 1752dff9b4
commit 77961345a8

@ -1,3 +1,17 @@
from django.contrib import admin from django.contrib import admin
from .models import Category, Location, Organization, OrganizationImage, Region
# Register your models here. # Register your models here.
models = [Category, Location, OrganizationImage, Region]
for model in models:
admin.site.register(model)
@admin.register(Organization)
class OrganizationAdmin(admin.ModelAdmin):
list_display = ['name', 'category', 'phone', 'website']
inlines = [OrganizationImage]

@ -8,26 +8,39 @@ class Region(models.Model):
code = models.PositiveIntegerField() code = models.PositiveIntegerField()
name = models.CharField(max_length=64) name = models.CharField(max_length=64)
def __str__(self):
return self.name
class Location(models.Model): class Location(models.Model):
coords = PointField(db_index=True) coords = PointField(db_index=True)
region = models.ForeignKey(Region, on_delete=models.SET_NULL, null=True) region = models.ForeignKey(Region, on_delete=models.SET_NULL, null=True)
address = models.CharField(max_length=512, blank=True, null=True) address = models.CharField(max_length=512, blank=True, null=True)
def __str__(self):
return str(self.coords)
class Category(models.Model): class Category(models.Model):
name = models.CharField(max_length=64) name = models.CharField(max_length=64)
icon = models.ImageField(upload_to='icons/category/') icon = models.ImageField(upload_to='icons/category/')
def __str__(self):
return self.name
class Organization(models.Model): class Organization(models.Model):
location = models.ForeignKey(Location, on_delete=models.SET_NULL, null=True) location = models.ForeignKey(Location, on_delete=models.SET_NULL, null=True)
name = models.CharField(max_length=256) name = models.CharField(max_length=256)
phone = models.CharField(max_length=20) phone = models.CharField(max_length=20)
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, default=None)
website = models.URLField() website = models.URLField()
description = models.TextField() description = models.TextField()
owner = models.ForeignKey('jwtauth.CustomUser', on_delete=models.SET_NULL, null=True) owner = models.ForeignKey('jwtauth.CustomUser', on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.name
class OrganizationImage(models.Model): class OrganizationImage(models.Model):
image = models.ImageField(upload_to='images/organizations') image = models.ImageField(upload_to='images/organizations')

@ -3,7 +3,7 @@ from rest_framework import serializers
from core.fields import LocationField from core.fields import LocationField
from jwtauth.serializers import CustomUserSerializer from jwtauth.serializers import CustomUserSerializer
from .models import Category, Location, Organization, OrganizationImage from .models import Category, Location, Organization, OrganizationImage
from .repositories import OrganizationImagesRepository from .repositories import OrganizationImagesRepository, CategoryRepository
class CategorySerializer(serializers.ModelSerializer): class CategorySerializer(serializers.ModelSerializer):
@ -28,14 +28,15 @@ class OrganizationListSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Organization model = Organization
fields = ('location', 'images', 'name', 'phone', 'description', 'website') fields = ('location', 'images', 'name', 'phone', 'description', 'website', 'category')
class OrganizationCreateSerializer(serializers.ModelSerializer): class OrganizationCreateSerializer(serializers.ModelSerializer):
location = LocationSerializer() location = LocationSerializer()
images = serializers.PrimaryKeyRelatedField(many=True, queryset=OrganizationImagesRepository) images = serializers.PrimaryKeyRelatedField(many=True, queryset=OrganizationImagesRepository)
owner = serializers.StringRelatedField() owner = serializers.StringRelatedField()
category = serializers.PrimaryKeyRelatedField(queryset=CategoryRepository.all())
class Meta: class Meta:
model = Organization model = Organization
fields = ('location', 'images', 'name', 'phone', 'website', 'description', 'owner') fields = ('location', 'images', 'name', 'phone', 'website', 'description', 'owner', 'category')

Loading…
Cancel
Save