Here is a basic implementation of the Cow and Bull Management System using Django:
Models
# models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
ROLE_CHOICES = [
('admin', 'Admin'),
('manager', 'Manager'),
('vet', 'Vet'),
]
role = models.CharField(max_length=10, choices=ROLE_CHOICES, default='manager')
class Animal(models.Model):
TAG_ID_CHOICES = [
('cow', 'Cow'),
('bull', 'Bull'),
]
BREED_CHOICES = [
('holstein', 'Holstein'),
('jersey', 'Jersey'),
('other', 'Other'),
]
TYPE_CHOICES = [
('dairy', 'Dairy'),
('beef', 'Beef'),
]
STATUS_CHOICES = [
('active', 'Active'),
('inactive', 'Inactive'),
]
tag_id = models.CharField(max_length=10, choices=TAG_ID_CHOICES)
breed = models.CharField(max_length=10, choices=BREED_CHOICES)
type = models.CharField(max_length=10, choices=TYPE_CHOICES)
weight = models.DecimalField(max_digits=5, decimal_places=2)
status = models.CharField(max_length=10, choices=STATUS_CHOICES)
def __str__(self):
return f"{self.tag_id} - {self.breed}"
class MilkProductionLog(models.Model):
animal = models.ForeignKey(Animal, on_delete=models.CASCADE)
date = models.DateField()
milk_yield = models.DecimalField(max_digits=5, decimal_places=2)
def __str__(self):
return f"{self.animal} - {self.date}"
class HealthAndVetHistory(models.Model):
animal = models.ForeignKey(Animal, on_delete=models.CASCADE)
date = models.DateField()
description = models.TextField()
def __str__(self):
return f"{self.animal} - {self.date}"
class BreedingRecord(models.Model):
animal = models.ForeignKey(Animal, on_delete=models.CASCADE)
date = models.DateField()
description = models.TextField()
def __str__(self):
return f"{self.animal} - {self.date}"
class FarmExpense(models.Model):
date = models.DateField()
description = models.TextField()
amount = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
return f"{self.date} - {self.description}"
Views
# views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .models import Animal, MilkProductionLog, HealthAndVetHistory, BreedingRecord, FarmExpense
from .forms import AnimalForm, MilkProductionLogForm, HealthAndVetHistoryForm, BreedingRecordForm, FarmExpenseForm
@login_required
def dashboard(request):
animals = Animal.objects.all()
milk_production_logs = MilkProductionLog.objects.all()
farm_expenses = FarmExpense.objects.all()
return render(request, 'dashboard.html', {'animals': animals, 'milk_production_logs': milk_production_logs, 'farm_expenses': farm_expenses})
@login_required
def animal_list(request):
animals = Animal.objects.all()
return render(request, 'animal_list.html', {'animals': animals})
@login_required
def animal_detail(request, pk):
animal = Animal.objects.get(pk=pk)
return render(request, 'animal_detail.html', {'animal': animal})
@login_required
def milk_production_log_list(request):
milk_production_logs = MilkProductionLog.objects.all()
return render(request, 'milk_production_log_list.html', {'milk_production_logs': milk_production_logs})
@login_required
def milk_production_log_detail(request, pk):
milk_production_log = MilkProductionLog.objects.get(pk=pk)
return render(request, 'milk_production_log_detail.html', {'milk_production_log': milk_production_log})
@login_required
def health_and_vet_history_list(request):
health_and_vet_histories = HealthAndVetHistory.objects.all()
return render(request, 'health_and_vet_history_list.html', {'health_and_vet_histories': health_and_vet_histories})
@login_required
def health_and_vet_history_detail(request, pk):
health_and_vet_history = HealthAndVetHistory.objects.get(pk=pk)
return render(request, 'health_and_vet_history_detail.html', {'health_and_vet_history': health_and_vet_history})
@login_required
def breeding_record_list(request):
breeding_records = BreedingRecord.objects.all()
return render(request, 'breeding_record_list.html', {'breeding_records': breeding_records})
@login_required
def breeding_record_detail(request, pk):
breeding_record = BreedingRecord.objects.get(pk=pk)
return render(request, 'breeding_record_detail.html', {'breeding_record': breeding_record})
@login_required
def farm_expense_list(request):
farm_expenses = FarmExpense.objects.all()
return render(request, 'farm_expense_list.html', {'farm_expenses': farm_expenses})
@login_required
def farm_expense_detail(request, pk):
farm_expense = FarmExpense.objects.get(pk=pk)
return render(request, 'farm_expense_detail.html', {'farm_expense': farm_expense})
@login_required
def create_animal(request):
if request.method == 'POST':
form = AnimalForm(request.POST)
if form.is_valid():
form.save()
return redirect('animal_list')
else:
form = AnimalForm()
return render(request, 'create_animal.html', {'form': form})
@login_required
def create_milk_production_log(request):
if request.method == 'POST':
form = MilkProductionLogForm(request.POST)
if form.is_valid():
form.save()
return redirect('milk_production_log_list')
else:
form = MilkProductionLogForm()
return render(request, 'create_milk_production_log.html', {'form': form})
@login_required
def create_health_and_vet_history(request):
if request.method == 'POST':
form = HealthAndVetHistoryForm(request.POST)
if form.is_valid():
form.save()
return redirect('health_and_vet_history_list')
else:
form = HealthAndVetHistoryForm()
return render(request, 'create_health_and_vet_history.html', {'form': form})
@login_required
def create_breeding_record(request):
if request.method == 'POST':
form = BreedingRecordForm(request.POST)
if form.is_valid():
form.save()
return redirect('breeding_record_list')
else:
form = BreedingRecordForm()
return render(request, 'create_breeding_record.html', {'form': form})
@login_required
def create_farm_expense(request):
if request.method == 'POST':
form = FarmExpenseForm(request.POST)
if form.is_valid():
form.save()
return redirect('farm_expense_list')
else:
form = FarmExpenseForm()
return render(request, 'create_farm_expense.html', {'form': form})
Forms
# forms.py
from django import forms
from .models import Animal, MilkProductionLog, HealthAndVetHistory, BreedingRecord, FarmExpense
class AnimalForm(forms.ModelForm):
class Meta:
model = Animal
fields = ('tag_id', 'breed', 'type', 'weight', 'status')
class MilkProductionLogForm(forms.ModelForm):
class Meta:
model = MilkProductionLog
fields = ('animal', 'date', 'milk_yield')
class HealthAndVetHistoryForm(forms.ModelForm):
class Meta:
model = HealthAndVetHistory
fields = ('animal', 'date', 'description')
class BreedingRecordForm(forms.ModelForm):
class Meta:
model = BreedingRecord
fields = ('animal', 'date', 'description')
class FarmExpenseForm(forms.ModelForm):
class Meta:
model = FarmExpense
fields = ('date', 'description', 'amount')
Admin Panel Configuration
# admin.py
from django.contrib import admin
from .models import Animal, MilkProductionLog, HealthAndVetHistory, BreedingRecord, FarmExpense
admin.site.register(Animal)
admin.site.register(MilkProductionLog)
admin.site.register(HealthAndVetHistory)
admin.site.register(BreedingRecord)
admin.site.register(FarmExpense)
Templates
Create the following templates in the templates directory:
base.html: a basic template with a navigation menu and a content area
dashboard.html: a template that displays the dashboard with charts and export options
animal_list.html: a template that displays a list of animals
animal_detail.html: a template that displays the details of a single animal
milk_production_log_list.html: a template that displays a list of milk production logs
milk_production_log_detail.html: a template that displays the details of a single milk production log
health_and_vet_history_list.html: a template that displays a list of health and vet histories
health_and_vet_history_detail.html: a template that displays the details of a single health and vet history
breeding_record_list.html: a template that displays a list of breeding records
breeding_record_detail.html: a template that displays the details of a single breeding record
farm_expense_list.html: a template that displays a list of farm expenses
farm_expense_detail.html: a template that displays the details of a single farm expense
create_animal.html: a template that displays a form to create a new animal
create_milk_production_log.html: a