Compare commits

..

2 commits

Author SHA1 Message Date
willy
fc35b28704 Before 20250302 2025-03-02 10:43:19 +01:00
willy
cbc6ad4e70 Fix .gitignore and untrack django_error.log 2024-12-22 03:20:01 +01:00
6 changed files with 52 additions and 148072 deletions

3
.gitignore vendored
View file

@ -19,3 +19,6 @@ static/
# Ignore staticfiles directory # Ignore staticfiles directory
**/staticfiles/ **/staticfiles/
staticfiles/ staticfiles/
# Ignore logs
*.log

View file

View file

@ -1,7 +1,33 @@
from django.contrib import admin from django.contrib import admin
from .models import Plant from .models import Plant, PlantImage
# Inline class for handling images
class PlantImageInline(admin.TabularInline):
model = PlantImage
extra = 3 # Display three empty forms for adding new images
fields = ('image',) # Only include fields that exist on PlantImage
can_delete = True # Allow deletion of images
max_num = 10 # Limit to 10 images per plant (optional)
@admin.register(Plant) @admin.register(Plant)
class PlantAdmin(admin.ModelAdmin): class PlantAdmin(admin.ModelAdmin):
list_display = ('name', 'image') list_display = ('id', 'name', 'description', 'get_images', 'buy_price', 'sell_price', 'stock')
search_fields = ('name',) search_fields = ('name','description') # Add search functionality
inlines = [PlantImageInline] # Add inline editing for images
def save_related(self, request, form, formsets, change):
super().save_related(request, form, formsets, change)
# Ensure all related PlantImage objects are saved correctly
for formset in formsets:
if hasattr(formset, 'save'):
formset.save()
def get_images(self, obj):
"""Display the first image or a placeholder in the list view."""
images = obj.images.all() # Access related images via the 'images' related_name
if images.exists():
return images[0].image.url # Show URL of the first image
return "No Images"
get_images.short_description = "Images" # Header for the admin column

View file

@ -1,14 +1,12 @@
# catalog/models.py
from django.db import models from django.db import models
class Plant(models.Model): class Plant(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
description = models.TextField(null=True, blank=True) # Allow null and blank description = models.TextField(null=True, blank=True)
image = models.ImageField(upload_to='plants/', null=True, blank=True) # Optional image buy_price = models.FloatField(null=True, blank=True)
buy_price = models.FloatField(null=True, blank=True) # Allow null for buy price sell_price = models.FloatField(null=True, blank=True)
sell_price = models.FloatField(null=True, blank=True) # Allow null for sell price stock = models.IntegerField(default=0)
stock = models.IntegerField(default=0) # Default to 0
def __str__(self): class PlantImage(models.Model):
return self.name plant = models.ForeignKey(Plant, related_name='images', on_delete=models.CASCADE)
image = models.ImageField(upload_to='plants/')

View file

@ -1,17 +1,20 @@
# catalog/serializers.py
from rest_framework import serializers from rest_framework import serializers
from .models import Plant from .models import Plant, PlantImage
class PlantImageSerializer(serializers.ModelSerializer):
class Meta:
model = PlantImage
fields = ['id', 'image'] # Fields to expose for each image
class PlantSerializer(serializers.ModelSerializer): class PlantSerializer(serializers.ModelSerializer):
images = PlantImageSerializer(many=True, read_only=True) # Include related images
class Meta: class Meta:
model = Plant model = Plant
fields = ['id', 'name', 'description', 'image', 'buy_price', 'sell_price', 'stock'] # Include all fields fields = ['id', 'name', 'description', 'buy_price', 'sell_price', 'stock', 'images'] # Add the `images` field
extra_kwargs = { extra_kwargs = {
'buy_price': {'required': False, 'allow_null': True}, # Allow null for buy price 'buy_price': {'required': False, 'allow_null': True},
'sell_price': {'required': False, 'allow_null': True}, # Allow null for sell price 'sell_price': {'required': False, 'allow_null': True},
'image': {'required': False, 'allow_null': True}, # Allow image to be optional 'description': {'required': False, 'allow_null': True},
'description': {'required': False, 'allow_null': True}, # Optional description
} }

File diff suppressed because it is too large Load diff