Compare commits

..

No commits in common. "fc35b28704a982d9d184bf0367d45fe72d8826c9" and "551ae8691b5fbb00a041afea225187bc56b684f6" have entirely different histories.

6 changed files with 148072 additions and 52 deletions

3
.gitignore vendored
View file

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

0
django_error.log Executable file
View file

View file

@ -1,33 +1,7 @@
from django.contrib import admin from django.contrib import admin
from .models import Plant, PlantImage from .models import Plant
# 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 = ('id', 'name', 'description', 'get_images', 'buy_price', 'sell_price', 'stock') list_display = ('name', 'image')
search_fields = ('name','description') # Add search functionality search_fields = ('name',)
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,12 +1,14 @@
# 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) description = models.TextField(null=True, blank=True) # Allow null and blank
buy_price = models.FloatField(null=True, blank=True) image = models.ImageField(upload_to='plants/', null=True, blank=True) # Optional image
sell_price = models.FloatField(null=True, blank=True) buy_price = models.FloatField(null=True, blank=True) # Allow null for buy price
stock = models.IntegerField(default=0) sell_price = models.FloatField(null=True, blank=True) # Allow null for sell price
stock = models.IntegerField(default=0) # Default to 0
class PlantImage(models.Model): def __str__(self):
plant = models.ForeignKey(Plant, related_name='images', on_delete=models.CASCADE) return self.name
image = models.ImageField(upload_to='plants/')

View file

@ -1,20 +1,17 @@
# catalog/serializers.py
from rest_framework import serializers from rest_framework import serializers
from .models import Plant, PlantImage from .models import Plant
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', 'buy_price', 'sell_price', 'stock', 'images'] # Add the `images` field fields = ['id', 'name', 'description', 'image', 'buy_price', 'sell_price', 'stock'] # Include all fields
extra_kwargs = { extra_kwargs = {
'buy_price': {'required': False, 'allow_null': True}, 'buy_price': {'required': False, 'allow_null': True}, # Allow null for buy price
'sell_price': {'required': False, 'allow_null': True}, 'sell_price': {'required': False, 'allow_null': True}, # Allow null for sell price
'description': {'required': False, 'allow_null': True}, 'image': {'required': False, 'allow_null': True}, # Allow image to be optional
'description': {'required': False, 'allow_null': True}, # Optional description
} }

148050
plant_catalog/django_error.log Executable file

File diff suppressed because it is too large Load diff