Model and admin

This commit is contained in:
Виктор Гладких 2019-10-02 14:50:30 +03:00
parent 753471544e
commit 7b18d7e430
3 changed files with 52 additions and 2 deletions

View File

@ -1,3 +1,9 @@
from django.contrib import admin
from rating import models
# Register your models here.
@admin.register(models.Rating)
class RatingAdmin(admin.ModelAdmin):
"""Rating type admin conf."""
list_display = ['name', 'ip']
list_display_links = ['name']

View File

@ -0,0 +1,28 @@
# Generated by Django 2.2.4 on 2019-10-02 11:32
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
]
operations = [
migrations.CreateModel(
name='Rating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('object_id', models.PositiveIntegerField()),
('ip', models.GenericIPAddressField(verbose_name='ip')),
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
],
options={
'verbose_name': 'rating',
},
),
]

View File

@ -1,3 +1,19 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.translation import gettext_lazy as _
# Create your models here.
class Rating(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
ip = models.GenericIPAddressField(verbose_name=_('ip'))
@property
def name(self):
# Check if Generic obj has name or title
if hasattr(self.content_object, 'name'):
return self.content_object.name
if hasattr(self.content_object, 'title'):
return self.content_object.title_translated