Reputation: 1368
I want to create a model in Django that can describes a garden with rows for plants. Every plant needs some room to grow and i want to describe how many plants would fit in a row - I am a little bit stuck on how to best describe this in Django:
from django.db import models
from django.contrib.auth.models import User
class Plant(models.Model):
name = models.CharField('Plantname', max_length=120)
size = models.PositiveIntegerField("area around plant in cm")
qty = models.PositiveIntegerField("number of plants")
color = models.CharField("color of blossom", max_length=20)
class Patch(models.Model):
FERT = "fertilized"
BIO = "biological"
METHODS = ((FERT, "Fertilized"), (BIO, "Biological"))
type = models.Charfield("kind of patch", max_length=20, choices=METHODS)
qty = models.PositiveIntegerField("number of patches")
size = models.PositiveIntegerField("length of patch in cm")
class task(models.Model):
task_id = models.PositiveIntegerField('Task ID')
person = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
patches = models.ManyToManyField(Patch)
plants = models.ManyToManyField(Plant)
Now I create:
and I want to assign a task to a person that has the number of patches and plants he needs to work on.
So when a task is created, I need to:
if number > max(plants)
can someone please help me out :)
Upvotes: 1
Views: 66
Reputation: 5483
Your models look fine. You'll have to ask for user input using Django Forms. You can probably use a ModelForm for your Task
model, then override the save()
method to add your business logic.
Also, you should rename class task
to class Task
(or better still class FarmingTask
since 'task' is a keyword used in several software libraries/languages and might conflict at a later time.)
Upvotes: 1