I've an app called lists.
This has 3 main models: List, ListItem and School.
Every list could be related to 1 school, or this field could be empty.
But when I'm trying to update my model List to have a school field I'm getting:
ImportError: cannot import name 'School' from 'lists.models' (D:\web_proyects\scolarte\lists\models.py)
(scolarte)
Even thought both models are in the same models.py file.
I've tried:
from .models import School
And:
from lists.models import School
lists/models.py:
from django.db import models
from products.models import Product
from roles.models import User
from .models import School
# Create your models here.
class List(models.Model):
LISTA_STATUS = (
('recibida_pagada', 'Recibida y pagada'),
('recibida_no_pagada', 'Recibida pero no pagada'),
('en_revision', 'En revision'),
('en_camino', 'En camino'),
('entregada', 'Entregada'),
('cancelada', 'Cancelada')
)
lista_id = models.CharField(max_length=100)
name = models.CharField(max_length=100)
user = models.OneToOneField(User, on_delete=models.CASCADE)
school = models.OneToOneField(School, on_delete=models.CASCADE)
status = models.CharField(max_length=20, choices=LISTA_STATUS, default='recibida_pagada')
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created_at']
def __str__(self):
return str(self.id)
class ListItem(models.Model):
lista = models.ForeignKey(List, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
comment = models.CharField(max_length=100, blank=True, null=True, default='')
uploaded_at = models.DateTimeField(auto_now_add=True)
step_two_complete = models.BooleanField(default=False)
def sub_total(self):
return int(self.product.price)
class School(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100, blank=False)
address_reference = models.CharField(max_length=100, blank=False)
provincia = models.CharField(max_length=100, blank=False, null=True)
canton = models.CharField(max_length=100, blank=False, null=True)
parroquia = models.CharField(max_length=100, blank=False, null=True)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created_at']
def __str__(self):
return str(self.name)
You don't need to import School since its already in the same model. However you do need to define School first before you can then reference it in another class in the same model file. Update your model so School is defined before List
from django.db import models
from products.models import Product
from roles.models import User
from .models import School
# Create your models here.
class School(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100, blank=False)
address_reference = models.CharField(max_length=100, blank=False)
provincia = models.CharField(max_length=100, blank=False, null=True)
canton = models.CharField(max_length=100, blank=False, null=True)
parroquia = models.CharField(max_length=100, blank=False, null=True)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created_at']
def __str__(self):
return str(self.name)
class List(models.Model):
LISTA_STATUS = (
('recibida_pagada', 'Recibida y pagada'),
('recibida_no_pagada', 'Recibida pero no pagada'),
('en_revision', 'En revision'),
('en_camino', 'En camino'),
('entregada', 'Entregada'),
('cancelada', 'Cancelada')
)
lista_id = models.CharField(max_length=100)
name = models.CharField(max_length=100)
user = models.OneToOneField(User, on_delete=models.CASCADE)
school = models.OneToOneField(School, on_delete=models.CASCADE)
status = models.CharField(max_length=20, choices=LISTA_STATUS, default='recibida_pagada')
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created_at']
def __str__(self):
return str(self.id)
class ListItem(models.Model):
lista = models.ForeignKey(List, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
comment = models.CharField(max_length=100, blank=True, null=True, default='')
uploaded_at = models.DateTimeField(auto_now_add=True)
step_two_complete = models.BooleanField(default=False)
def sub_total(self):
return int(self.product.price)
Related
I am trying to filter my many to many variation fields with respect to the product. means, I only want the variations related to the current product to show in the admin page. now its showing all the variations available for every product.
I added formfield_for_manytomany() function to my admin.py but how can I get the current product(id) in the cart or order to filter the variations?
most of the questions in stack overflow Is based on the current user, which is easy to get? but how should I get the specific product(id) that is opened in the admin panel.
admin.py
from django.contrib import admin
from .models import *
from products.models import Variation
class CartAdmin(admin.ModelAdmin):
list_display = ('cart_id', 'date_created')
class CartItemAdmin(admin.ModelAdmin):
list_display = ('user','cart', 'product', 'quantity','is_active')
def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == "variation":
product = Products.objects.get(id='??') # how I get the current product in the cart or order
kwargs["queryset"] = Variation.objects.filter(product=product.id)
return super().formfield_for_manytomany(db_field, request, **kwargs)
admin.site.register(Cart, CartAdmin)
admin.site.register(CartItem, CartItemAdmin)
CartItem Model
class CartItem(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True)
cart = models.ForeignKey(Cart, on_delete=models.CASCADE, null=True)
product = models.ForeignKey(Products, on_delete=models.CASCADE)
variation = models.ManyToManyField(Variation, blank=True)
quantity = models.IntegerField()
is_active = models.BooleanField(default=True)
created_date = models.DateTimeField(auto_now_add=True)
def item_total(self):
return self.product.price * self.quantity
def __str__(self):
return self.product.name
Product and Variation Model
class Products(models.Model):
name = models.CharField(max_length=50, unique=True)
slug = AutoSlugField(populate_from='name', max_length=100, unique=True)
isbn = models.CharField(max_length=20, unique=True, blank=True, null=True)
sub_category = models.ForeignKey(SubCategory, on_delete=models.CASCADE)
language = models.ForeignKey(Language, on_delete=models.SET_NULL, null=True)
author = models.CharField(max_length=100)
Publisher = models.CharField(max_length=100, blank=True, default=None)
release_date = models.DateField(blank=True, null=True, default=None)
price = models.IntegerField(default=None)
stock = models.IntegerField(default=None)
is_available = models.BooleanField(default=True)
cover_image = models.ImageField(upload_to='images/products')
image1 = models.ImageField(upload_to='images/products', blank=True, default=None, null=True)
image2 = models.ImageField(upload_to='images/products', blank=True, default=None, null=True)
image3 = models.ImageField(upload_to='images/products', blank=True, default=None, null=True)
description = models.TextField(max_length=2000, blank=True, default=None)
create_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
number_of_pages = models.IntegerField(blank=True, null=True)
weight = models.IntegerField(blank=True, null=True)
width = models.IntegerField(blank=True, null=True)
height = models.IntegerField(blank=True, null=True)
spine_width = models.IntegerField(blank=True, null=True)
class Meta:
verbose_name = 'Product'
verbose_name_plural = 'Products'
def get_url(self):
return reverse('product-view', args=[self.slug])
def __str__(self):
return self.name
class Variation(models.Model):
product = models.ForeignKey(Products, on_delete=models.CASCADE)
variation_category = models.CharField(max_length=100, choices=variation_category_choice)
variation_value = models.CharField(max_length=100, choices=variation_value_choice)
is_available = models.BooleanField(default=True)
date_added = models.DateTimeField(auto_now_add=True)
objects = VariationManager()
def __str__(self):
return self.variation_value
i want to add in only one user from Available classic to Chosen classic but not from admin page from view when user click on button
I tried this
pro = Profile(user=request.user)
pro.classic.add(name='Frank Sinatra-i love you baby')
but i get tthis error
Before adding any objects to a many-to-many relation, the both side of many-to-many objects must obtain their id, so they should be saved first.
Another problem in this code is you need to construct a Classic object before you add it to chosen ones. The add method receives objects, not parameters of model constructor.
May following code example helps you:
pro, _ = Profile.objects.get_or_create(user=request.user)
your_classic = Classic.objects.create(name='Frank Sinatra-i love you baby')
pro.classic.add(your_classic)
give me this error
mu model code
from django.db import models
# Create your models here.
from django.utils import timezone
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class RequestSong(models.Model):
music = models.CharField(max_length=150, blank=False, null=False)
stampDate = models.DateTimeField(default=timezone.now, blank=False, null=False)
def __str__(self):
return self.music
class HelpUser(models.Model):
email = models.EmailField(blank=False, null=False)
helpMessege = models.TextField(blank=False, null=False)
stampDate = models.DateTimeField(default=timezone.now)
def __str__(self):
return str(self.email)
class Language(models.Model):
language = models.CharField(
max_length=2,
choices=[
('AR', 'Arabic'),
('EN', 'English'),
],
default='AR'
)
def __str__(self):
return self.language
class ChillOut(models.Model):
name = models.CharField(max_length=50, blank=False, null=False)
music = models.FileField(upload_to='', max_length=100, blank=True, null=True)
lang = models.ForeignKey(Language, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Sad(models.Model):
name = models.CharField(max_length=50, blank=False, null=False)
music = models.FileField(upload_to='', max_length=100, blank=True, null=True)
lang = models.ForeignKey(Language, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Happy(models.Model):
name = models.CharField(max_length=50, blank=False, null=False)
music = models.FileField(upload_to='', max_length=100, blank=True, null=True)
lang = models.ForeignKey(Language, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Romantic(models.Model):
name = models.CharField(max_length=50, blank=False, null=False)
music = models.FileField(upload_to='', max_length=100, blank=True, null=True)
lang = models.ForeignKey(Language, on_delete=models.CASCADE)
def __str__(self):
return self.name
class WorkOut(models.Model):
name = models.CharField(max_length=50, blank=False, null=False)
music = models.FileField(upload_to='', max_length=100, blank=True, null=True)
lang = models.ForeignKey(Language, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Classic(models.Model):
name = models.CharField(max_length=50, blank=False, null=False)
music = models.FileField(upload_to='', max_length=100, blank=True, null=True)
lang = models.ForeignKey(Language, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
classic = models.ManyToManyField(Classic, blank=True, null=True)
workOut = models.ManyToManyField(WorkOut, blank=True, null=True)
chillOut = models.ManyToManyField(ChillOut, blank=True, null=True)
romantic = models.ManyToManyField(Romantic, blank=True, null=True)
happy = models.ManyToManyField(Happy, blank=True, null=True)
sad = models.ManyToManyField(Sad, blank=True, null=True)
def __str__(self):
return str(self.user)
# class Favorite(models.Model):
# name = models.CharField(max_length=50, blank=False, null=False)
# fav = models.ForeignKey(ChillOut, on_delete=models.CASCADE)
# pro = models.ForeignKey(Profile, on_delete=models.CASCADE)
# def __str__(self):
# return self.name
def update_user_profile(sender, **kwargs):
if kwargs['created']:
user = Profile.objects.create(user=kwargs['instance'])
post_save.connect(update_user_profile,sender=User)
I'm fresh of the django boat, working on a project that need to do 'one to many'. Here's what I'm trying to make my admin dashboard look like:
Patients
Patient001
Profile
Record
Inputoutput
Day1
Day2
Patient002
Profile
Record
Inputoutput
Day1
The "Patient", "Profile", "Record", "Inputoutput" are all models and related with a foreign key "name" in "Patient" model. I hope to achieve to add or edit the data in each model by clicking in folders on django admin dashboard. How should I do this. I've been googling for days. What I found that looks like I'm looking for is a "custom django admin dashboard", but still not sure how to do this. Anyone has an idea? =)
What I want to achieve is:
first>> create a "Patient" model (admin create a Patient Folder shows on dashboard)
then>> create a "Profile" model (click into the Patient Folder we just create and add a Profile model)
I want all these created models to show in levels of folders on admin, and you can just click in to the folders to add or edit instead of all of the registered models showing together on the very first page of admin.
Here are my models:
--patient.py--
from django.db import models
class Patient(models.Model):
name = models.CharField(default="", max_length=200)
def __str__(self):
return self.name
class Meta:
app_label = 'vsform'
--profile.py--
from django.db import models
from django.utils import timezone
import datetime
class Profile(models.Model):
patient = models.ForeignKey('Patient', on_delete=models.CASCADE)
name = models.CharField(max_length=200)
record_number = models.CharField(max_length=200)
bed_number = models.CharField(max_length=200)
gender = models.CharField(max_length=1, choices=(('男', '男'), ('女', '女')))
operation_date = models.DateTimeField(default=timezone.now)
chart_start_time = models.IntegerField(default=4)
class Meta:
app_label = 'vsform'
def __str__(self):
return self.name
--record.py--
from django.db import models
class Record(models.Model):
patient = models.ForeignKey('Patient', on_delete=models.CASCADE)
blood_pressure_day1_1 = models.CharField(max_length=200, blank=True)
blood_pressure_day1_2 = models.CharField(max_length=200, blank=True)
blood_pressure_day2_1 = models.CharField(max_length=200, blank=True)
blood_pressure_day2_2 = models.CharField(max_length=200, blank=True)
blood_pressure_day3_1 = models.CharField(max_length=200, blank=True)
blood_pressure_day3_2 = models.CharField(max_length=200, blank=True)
blood_pressure_day4_1 = models.CharField(max_length=200, blank=True)
blood_pressure_day4_2 = models.CharField(max_length=200, blank=True)
blood_pressure_day5_1 = models.CharField(max_length=200, blank=True)
blood_pressure_day5_2 = models.CharField(max_length=200, blank=True)
height_day1 = models.CharField(max_length=200, blank=True)
weight_day1 = models.CharField(max_length=200, blank=True)
height_day2 = models.CharField(max_length=200, blank=True)
weight_day2 = models.CharField(max_length=200, blank=True)
height_day3 = models.CharField(max_length=200, blank=True)
weight_day3 = models.CharField(max_length=200, blank=True)
height_day4 = models.CharField(max_length=200, blank=True)
weight_day4 = models.CharField(max_length=200, blank=True)
height_day5 = models.CharField(max_length=200, blank=True)
weight_day5 = models.CharField(max_length=200, blank=True)
special_drug1 = models.CharField(max_length=200, blank=True)
special_drug2 = models.CharField(max_length=200, blank=True)
special_drug3 = models.CharField(max_length=200, blank=True)
special_drug4 = models.CharField(max_length=200, blank=True)
exam1 = models.CharField(max_length=200, blank=True)
exam2 = models.CharField(max_length=200, blank=True)
exam3 = models.CharField(max_length=200, blank=True)
exam4 = models.CharField(max_length=200, blank=True)
exam5 = models.CharField(max_length=200, blank=True)
exam6 = models.CharField(max_length=200, blank=True)
exam7 = models.CharField(max_length=200, blank=True)
exam8 = models.CharField(max_length=200, blank=True)
exam9 = models.CharField(max_length=200, blank=True)
exam10 = models.CharField(max_length=200, blank=True)
def __str__(self):
return self.patient.name
class Meta:
app_label = 'vsform'
--inputoutput.py--
from django.db import models
class InputOutput(models.Model):
patient = models.ForeignKey('Patient', on_delete=models.CASCADE)
def __str__(self):
return self.patient.name
class Meta:
app_label = 'vsform'
--day.py--
from django.db import models
class Day(models.Model):
inputoutput = models.ForeignKey('InputOutput', on_delete=models.CASCADE)
day_number = models.SmallIntegerField(default=0)
injection_7to3 = models.IntegerField(default=0)
injection_3to11 = models.IntegerField(default=0)
injection_11to7 = models.IntegerField(default=0)
food_7to3 = models.IntegerField(default=0)
food_3to11 = models.IntegerField(default=0)
food_11to7 = models.IntegerField(default=0)
transfusion_7to3 = models.IntegerField(default=0)
transfusion_3to11 = models.IntegerField(default=0)
transfusion_11to7 = models.IntegerField(default=0)
excrete_times_7to3 = models.SmallIntegerField(default=0)
excrete_times_3to11 = models.SmallIntegerField(default=0)
excrete_times_11to7 = models.SmallIntegerField(default=0)
urine_7to3 = models.IntegerField(default=0)
urine_3to11 = models.IntegerField(default=0)
urine_11to7 = models.IntegerField(default=0)
vomit_7to3 = models.IntegerField(default=0)
vomit_3to11 = models.IntegerField(default=0)
vomit_11to7 = models.IntegerField(default=0)
drain_7to3 = models.IntegerField(default=0)
drain_3to11 = models.IntegerField(default=0)
drain_11to7 = models.IntegerField(default=0)
def __str__(self):
return "Day " + str(self.day_number)
class Meta:
app_label = 'vsform'
And here's admin.py
--admin.py--
from django.contrib import admin
from .models.patient import Patient
admin.site.register(Patient)
I want to get authorName whenever someone search for a book and yeah I have tried many to many but it is very confusing and after researching some things I got to a view which I thought should work fine but it is returning an error "1242, 'Subquery returns more than 1 row'" here are some of the relevant code:
Models
from __future__ import unicode_literals
from django.db import models
class Books(models.Model):
bid = models.BigIntegerField(primary_key=True)
bname = models.CharField(max_length=200, blank=True, null=True)
bdescription = models.TextField(blank=True, null=True)
def __str__(self):
return self.bname
class Authors(models.Model):
aid = models.AutoField(primary_key=True)
aname = models.CharField(max_length=200, blank=True, null=True)
adescription = models.TextField( blank=True, null=True)
def __str__(self):
return self.aname+"\n"
class Bookauth(models.Model):
bid = models.ForeignKey(Books, on_delete=models.DO_NOTHING, db_column='bid', blank=True, null=True)
aid = models.ForeignKey(Authors, on_delete=models.DO_NOTHING, db_column='aid', blank=True, null=True)
Views for what I thought was right
def authbook(request):
s = Authors.objects.all()
r = Books.objects.filter(bookauth__aid = s).values()
return HttpResponse(r)
I have a simple model, witch is used as a form .
class Test(models.Model):
name = models.CharField(max_length=100, unique=True, db_index=True)
location = models.CharField(max_length=300)
details = models.TextField()
def __unicode__(self):
return self.image.name
I would like to add the following class Album as a foreign key to Test :
class Album(models.Model):
title = models.CharField(max_length=60)
public = models.BooleanField(default=False)
def __unicode__(self):
return self.title
class Tag(models.Model):
tag = models.CharField(max_length=50)
def __unicode__(self):
return self.tag
class Image(models.Model):
title = models.CharField(max_length=60, blank=True, null=True)
image = models.FileField(upload_to="images/")
tags = models.ManyToManyField(Tag, blank=True)
albums = models.ManyToManyField(Album, blank=True)
created = models.DateTimeField(auto_now_add=True)
rating = models.IntegerField(default=50)
width = models.IntegerField(blank=True, null=True)
height = models.IntegerField(blank=True, null=True)
user = models.ForeignKey(User, null=True, blank=True)
def __unicode__(self):
return self.image.name
Questions:
How to add class Album as a foreigh key to class Test?
How to put this relation on the form? - e.g. user is selecting multiple images for uploads wich results in unique Album related to Test class.
Do you mean something like this for the foreign-key
class Test(models.Model):
name = models.CharField(max_length=100, unique=True, db_index=True)
location = models.CharField(max_length=300)
details = models.TextField()
album = models.ForeignKey(Album, null=True, blank=True)
def __unicode__(self):
return self.name