I'm building a django app that has customers model and projects model and tasks model. in tasks model I can select the customer name and project but the problem is that in admin panel it shows all the projects, is there any way to show projects only for the selected customer
from django.db import models
from django.contrib.auth.models import User
from suppliers.models import Currency
from users.models import Profile
class Customer(models.Model):
customer_id = models.AutoField(primary_key=True)
customer_first_name = models.CharField(max_length=200)
customer_last_name = models.CharField(max_length=200)
company = models.CharField(max_length=200)
customer_phone = models.CharField(max_length=200)
customer_address = models.CharField(max_length=200)
email = models.EmailField(null=True, blank=True)
website = models.CharField(max_length=200, null=True, blank=True)
creation_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
notes = models.TextField()
def __str__(self):
return str(self.customer_first_name) + ' ' + str(self.customer_last_name)
class Account(models.Model):
max_discount = models.DecimalField(max_digits=2, decimal_places=2)
credit_limit = models.DecimalField(max_digits=20, decimal_places=2)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True, blank=True)
currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
sales_man = models.OneToOneField(User, on_delete=models.CASCADE)
Agent = models.OneToOneField(Profile, on_delete=models.CASCADE)
status = models.BooleanField(default=True)
reason = models.TextField()
class TaskPriority(models.Model):
priority_id = models.AutoField(primary_key=True)
task_priority_name = models.CharField(max_length=200)
def __str__(self):
return str(self.task_priority_name)
class Project(models.Model):
project_id = models.AutoField(primary_key=True)
project_name = models.CharField(max_length=200)
project_balance = models.DecimalField(max_digits=20, decimal_places=2)
customer_name = models.ForeignKey(Customer, on_delete=models.CASCADE)
creation_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
notes = models.TextField()
def __str__(self):
return str(self.project_name)
class Task(models.Model):
task_id = models.AutoField(primary_key=True)
task_name = models.CharField(max_length=200)
customer_name = models.ForeignKey(Customer, on_delete=models.CASCADE, blank=True, null=True)
task_priority = models.ForeignKey(TaskPriority, on_delete=models.CASCADE)
employee = models.ForeignKey(Profile, on_delete=models.CASCADE)
creation_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
project = models.ForeignKey(Project, null=True, blank=True, on_delete=models.CASCADE)
file_name = models.FileField(upload_to='projects_files')
notes = models.TextField()
def __str__(self):
return str(self.task_name)
You can use TabularInline to create project tab on customer record view in admin panel.
For example:
class ProjectInline(admin.TabularInline):
model = Project
extra = 1
verbose_name = 'Project'
verbose_name_plural = 'Projects'
list_display = ...
And in Customer Admin model add
class CustomerAdmin(admin.ModelAdmin):
...
inlines = (ProjectInline, )
Related
I have 3 models 1) university 2) courses and 3) enquiry.
My enquiry model has a foreign key to "course". And Course has a foreign key to the university. I only want to show the courses related to that university while adding an enquiry. I tried django-smart-select but failed to do so. I tried This answer but I dont understand the logic and failed to implement in my project.
this is my models.py file
class university(models.Model):
univ_name = models.CharField(max_length=100)
country = CountryField(blank=True, null=True)
univ_desc = models.CharField(max_length=1000)
univ_logo = models.ImageField(upload_to="media")
univ_phone = models.CharField(max_length=10, blank=True)
univ_email = models.EmailField(max_length=254, blank=True)
univ_website = models.URLField(blank=True)
assigned_users = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, default="")
def __str__(self):
return self.univ_name
class Meta:
verbose_name_plural = "Universities"
class Course(models.Model):
university = models.ForeignKey(university, on_delete=models.CASCADE)
course_name = models.CharField(max_length=100)
course_levels = models.ForeignKey(course_levels, on_delete=models.CASCADE)
intake = models.ForeignKey(intake, on_delete=models.CASCADE)
documents_required = models.ForeignKey(documents_required, on_delete=models.CASCADE)
course_requirements = models.ForeignKey(course_requirements, on_delete=models.CASCADE)
Active = models.BooleanField()
def __str__(self):
return self.course_name
class enquiry(models.Model):
student_name = models.CharField(max_length=100)
student_phone = models.CharField(max_length=10)
student_email = models.EmailField()
student_address = models.TextField()
current_education = models.ForeignKey(current_education, on_delete=models.CASCADE)
country_interested = CountryField(blank=True, null=True)
university_interested = models.ForeignKey(university, on_delete=models.CASCADE)
course_interested = models.ForeignKey(Course, on_delete=models.CASCADE, limit_choices_to={'Active':True})
level_applying_for = models.ForeignKey(course_levels, on_delete=models.CASCADE)
intake_interested = models.ForeignKey(intake, on_delete=models.CASCADE)
assigned_users = models.ForeignKey(User, on_delete=models.CASCADE, default="", limit_choices_to={"is_active": True})
enquiry_status = models.ForeignKey(enquiry_status, on_delete=models.CASCADE, default="")
course_interested= ChainedForeignKey(Course,chained_field= 'university_interested',chained_model_field= 'university',show_all= False,auto_choose= True,sort=True,limit_choices_to = {"Active": True},)
I want to show the course_interested field related to that university. Need help.
I tried using django-smart-select but failed to implement it. I am not aware of jquery and ajax so that is out of the question to use in my project.
got the solution I used the django-select2 and was able to solve this issue.
I'm trying to acheive catgeory and product offers in my project and am unable to come up with a solution. Like if i give offer to a category all products price in category should get the offer and for products its individual.
class Category(models.Model):
category_name = models.CharField(max_length=15, unique=True)
slug = models.SlugField(max_length=100, unique=True)
class Meta:
verbose_name_plural = "Category"
class Products(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
product_name = models.CharField(max_length=50, unique=True)
slug = models.SlugField(max_length=100, unique=True)
description = models.TextField(max_length=500)
price = models.IntegerField()
images = models.ImageField(upload_to="photos/products")
images_two = models.ImageField(upload_to="photos/products")
images_three = models.ImageField(upload_to="photos/products")
stock = models.IntegerField()
is_available = models.BooleanField(default=True)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = "Products"
def __str__(self):
return self.product_name
class CategoryOffer(models.Model):
category = models.OneToOneField(Category, on_delete=models.CASCADE, related_name='cat_offer')
valid_from = models.DateTimeField()
valid_to = models.DateTimeField()
discount = models.IntegerField(
validators=[MinValueValidator(1), MaxValueValidator(100)]
)
is_active = models.BooleanField(default=False)
def __str__(self):
return self.category.category_name
class ProductOffer(models.Model):
product = models.OneToOneField(Products, on_delete=models.CASCADE, related_name='pro_offer')
valid_from = models.DateTimeField()
valid_to = models.DateTimeField()
discount = models.IntegerField(
validators=[MinValueValidator(1), MaxValueValidator(100)]
)
is_active = models.BooleanField(default=False)
def __str__(self):
return self.product.product_name
So above are my models. I don't know how to implement, thought of many ways but it keeps leading to errors.
You are using separate models for Categoryoffer and Productoffer. Make an Offer model with the following field:
class Offer:
name = models.CharField()
valid_from = models.DateTimeField()
valid_to = models.DateTimeField()
discount = models.IntegerField(
validators=[
MinValueValidator(1),
MaxValueValidator(100)
])
is_active = models.BooleanField(default=False)
Now use the foreign key in your category and product models:
class Product:
offer = models.ForeignKey(Offer)
Filter A foreignkey child model instance in a form field based on the parent value selected in another field in the same field
class Agents(models.Model):
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete = models.CASCADE)
address = models.TextField()
gender = models.CharField(max_length=50)
numero_telephone = models.CharField(max_length=50)
departement_id = models.ForeignKey(Departements, on_delete=models.CASCADE, default=1)
role_id = models.ForeignKey(Roles, on_delete=models.CASCADE, default=1)
profile_pic = models.FileField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = models.Manager()
class Projets(models.Model):
id = models.AutoField(primary_key=True)
projet_name = models.CharField(max_length=255)
departement_id = models.ForeignKey(Departements, on_delete=models.CASCADE, default=1) #need to give defauult course
manager_id = models.CharField(max_length=255, null=True,blank=True)
membres_id = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = models.Manager()
i want company_name to be unique=True when company_is_deleted=False. Similarly when company_is_deleted=True then company_name to be unique=False. Where i am using soft delete means that i am just setting company_is_deleted=True and not deleting it from database table.
Company Model
class Company(models.Model):
company_name = models.CharField(max_length=20, unique=True) # Here
company_description = models.CharField(max_length=100)
company_address = models.CharField(max_length=100)
company_email = models.EmailField()
company_website = models.URLField()
company_phone = models.CharField(max_length=30)
company_monthly_payment = models.DecimalField(max_digits=5, decimal_places=2)
company_logo = models.ImageField(upload_to='company_logo', default='default_company.png',blank=True, null=True)
company_created = models.DateTimeField(auto_now_add=True)
company_is_deleted = models.BooleanField(default=False)
View.py
class CompanyCreateView(LoginRequiredMixin, generic.CreateView):
model = Company
fields = ['company_name', 'company_description', 'company_email',
'company_website', 'company_address', 'company_phone', 'company_status',
'company_monthly_payment', 'company_logo']
You can add that logic into save method. But remove unique from company_name field.
from django.db import IntegrityError
class Company(models.Model):
company_name = models.CharField(max_length=20)
company_description = models.CharField(max_length=100)
company_address = models.CharField(max_length=100)
company_email = models.EmailField()
company_website = models.URLField()
company_phone = models.CharField(max_length=30)
company_monthly_payment = models.DecimalField(max_digits=5, decimal_places=2)
company_logo = models.ImageField(upload_to='company_logo', default='default_company.png',blank=True, null=True)
company_created = models.DateTimeField(auto_now_add=True)
company_is_deleted = models.BooleanField(default=False)
def save(self, *args, **kwargs):
if not self.company_is_deleted and Company.objects.filter(
company_name=self.company_name,
company_is_deleted=False
).exists():
raise IntegrityError
super(Company, self).save(*args, **kwargs)
So i'm practicing django with a social media website(just for practice). I'm trying to do a filter in my view to get all of the "Beats" from users i'm friends with. Im using an intermediary table for "relationships". I'm currently getting my Beat Stream by using:
my_stream = Beat.objects.filter(artist=user)
But i'm trying to get only the beats of the people i'm "related_to" in the relationships model.
MODELS:
class Beat(models.Model):
created_at = models.DateTimeField( default=datetime.now)
title = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
likes = models.IntegerField(default=0)
artist = models.ForeignKey(User, null=True, blank=True)
beat_cover = models.ImageField(null=True, blank=True);
admin_name = models.CharField(max_length=255, blank=True, null=True)
class Meta:
ordering = ['-created_at']
def __unicode__(self):
return unicode(self.admin_name)
class UserProfile(models.Model):
user = models.OneToOneField(User)
admin_name = models.CharField(default="beat",max_length=255,blank=True, null=True)
def __unicode__(self):
return unicode(self.admin_name)
class Relationship(models.Model):
from_user = models.ForeignKey(User, related_name="relationships")
to_user = models.ForeignKey(User, related_name="related_to")
class Meta:
index_together = ['from_user','to_user']
You should do:
related_users = Relationship.objects.filter(from_user=user) \
.values_list('to_user', flat=True).distinct()
my_stream = Beat.objects.filter(artist__in=related_users)