xtlc
xtlc

Reputation: 1368

Django best practice models question (from a beginner)

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:

  1. two example patches with 300 and 400cm length
  2. two example plants with 20 and 15cm length

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:

  1. ask for the size and number of the patches
  2. Choose the type of plant
  3. Calculate max. amount of plants possible
  4. ask for the number of plants and set it max(plants) if number > max(plants)
  5. create a task with the total quantity of patches and plants

can someone please help me out :)

Upvotes: 1

Views: 66

Answers (1)

Nitin Nain
Nitin Nain

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

Related Questions