add photo relation

This commit is contained in:
j 2018-02-22 19:01:52 +05:30
parent fe13a100e0
commit d4809e29c1
3 changed files with 64 additions and 1 deletions

View File

@ -55,6 +55,7 @@ class ContentAdmin(admin.ModelAdmin):
list_display = ('id', '__str__', 'datestart', 'shortname', 'type') list_display = ('id', '__str__', 'datestart', 'shortname', 'type')
list_filter = ['datestart', 'type'] list_filter = ['datestart', 'type']
search_fields = ['title', 'body', 'header', 'shortname'] search_fields = ['title', 'body', 'header', 'shortname']
raw_id_fields = ['photo']
inlines = [ContentParentsInline, FileInline, LinkInline] inlines = [ContentParentsInline, FileInline, LinkInline]
formfield_overrides = { formfield_overrides = {
models.TextField: {'widget': AdminMarkdownxWidget}, models.TextField: {'widget': AdminMarkdownxWidget},

View File

@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2018-02-22 13:19
from __future__ import unicode_literals
from django.db import migrations, models
import photologue.models
def migrate_photos(apps, schema_editor):
Content = apps.get_model('content', 'Content')
Photo = apps.get_model('photologue', 'Photo')
for c in Content.objects.exclude(image=None).exclude(image=""):
if not c.photo:
try:
if '/photo' in c.image:
c.photo = Photo.objects.get(image__endswith=c.image.split('/')[-1])
else:
c.photo = Photo.objects.get(title=c.image)
except Photo.DoesNotExist:
continue
c.save()
class Migration(migrations.Migration):
dependencies = [
('content', '0009_auto_20171219_1320'),
('photologue', '0010_auto_20160105_1307'),
]
operations = [
migrations.AlterModelOptions(
name='contentcontent',
options={'verbose_name': 'related content', 'verbose_name_plural': 'related content'},
),
migrations.AlterModelOptions(
name='file',
options={'ordering': ['order', 'file']},
),
migrations.AlterModelOptions(
name='link',
options={'ordering': ['order', 'url']},
),
migrations.AddField(
model_name='content',
name='photo',
field=models.ForeignKey(blank=True, null=True, on_delete=photologue.models.Photo, related_name='main_photo', to='content.Image'),
),
migrations.AlterField(
model_name='content',
name='image',
field=models.CharField(blank=True, editable=False, max_length=150, null=True),
),
migrations.RunPython(migrate_photos),
]

View File

@ -76,7 +76,9 @@ class Content(models.Model):
optbtn3 = models.CharField(db_column='optBtn3', max_length=127, blank=True, null=True) # Field name made lowercase. optbtn3 = models.CharField(db_column='optBtn3', max_length=127, blank=True, null=True) # Field name made lowercase.
opttext3 = models.TextField(db_column='optText3', blank=True, null=True) # Field name made lowercase. opttext3 = models.TextField(db_column='optText3', blank=True, null=True) # Field name made lowercase.
technotes = models.TextField(db_column='technotes', blank=True, null=True) technotes = models.TextField(db_column='technotes', blank=True, null=True)
image = models.CharField(max_length=150, blank=True, null=True) image = models.CharField(max_length=150, blank=True, null=True, editable=False)
photo = models.ForeignKey('Image', Photo, null=True, blank=True, related_name="main_photo")
postedby = models.CharField(db_column='postedBy', max_length=50, blank=True, null=True) # Field name made lowercase. postedby = models.CharField(db_column='postedBy', max_length=50, blank=True, null=True) # Field name made lowercase.
datestart = models.DateField(db_column='dateStart', blank=True, null=True) # Field name made lowercase. datestart = models.DateField(db_column='dateStart', blank=True, null=True) # Field name made lowercase.
dateend = models.DateField(db_column='dateEnd', blank=True, null=True) # Field name made lowercase. dateend = models.DateField(db_column='dateEnd', blank=True, null=True) # Field name made lowercase.
@ -123,6 +125,8 @@ class Content(models.Model):
@property @property
def image_url(self): def image_url(self):
if self.photo.image.url:
return self.photo.image.url
if self.image: if self.image:
if self.image.startswith('http') or self.image.startswith('/'): if self.image.startswith('http') or self.image.startswith('/'):
return self.image return self.image
@ -174,6 +178,8 @@ class ContentContent(models.Model):
class Meta: class Meta:
# managed = False # managed = False
db_table = 'content_content' db_table = 'content_content'
verbose_name = 'related content'
verbose_name_plural = 'related content'
def reverse(self): def reverse(self):
r, created = ContentContent.objects.get_or_create(contentid1=self.contentid2, contentid2=self.contentid1) r, created = ContentContent.objects.get_or_create(contentid1=self.contentid2, contentid2=self.contentid1)