144
-
Variable lists, also known as a many-to-one relationship, are usually handled by making a separate model for the many and, in that model, using a ForeignKey to the “one”.
-
There isn’t an app like this in django.contrib, but there are several external projects you can use, e.g. django-photologue which even has some support for viewing the images in the admin.
-
The admin site can’t be made “user proof”, it should only be used by trusted users. Given this, the way to make your admin site decent would be to define a ModelAdmin for your property and then inline the photos (inline documentation).
So, to give you some quick drafts, everything would look something like this:
# models.py
class Property(models.Model):
address = models.TextField()
...
class PropertyImage(models.Model):
property = models.ForeignKey(Property, related_name='images')
image = models.ImageField()
and:
# admin.py
class PropertyImageInline(admin.TabularInline):
model = PropertyImage
extra = 3
class PropertyAdmin(admin.ModelAdmin):
inlines = [ PropertyImageInline, ]
admin.site.register(Property, PropertyAdmin)
The reason for using the related_name argument on the ForeignKey is so your queries will be more readable, e.g. in this case you can do something like this in your view:
property = Property.objects.get(pk=1)
image_list = property.images.all()
EDIT: forgot to mention, you can then implement drag-and-drop ordering in the admin using Simon Willison’s snippet Orderable inlines using drag and drop with jQuery UI
7
Write an Image model that has a ForeignKey to your Property model. Quite probably, you’ll have some other fields that belong to the image and not to the Property.
- [Django]-Django gives Bad Request (400) when DEBUG = False
- [Django]-Images from ImageField in Django don't load in template
- [Django]-How can I restrict Django's GenericForeignKey to a list of models?
4
I’m currently making the same thing and I faced the same issue.
After I researched for a while, I decided to use django-imaging. It has a nice Ajax feature, images can be uploaded on the same page as the model Insert page, and can be editable. However, it is lacking support for non-JPEG extension.
- [Django]-Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT
- [Django]-Add rich text format functionality to django TextField
- [Django]-Visual Editor for Django Templates?
- [Django]-Django reverse lookup of foreign keys
- [Django]-How to run celery as a daemon in production?
- [Django]-How to send html email with django with dynamic content in it?