I'm only a newbie in Python and Django.
I'm working with a MySQL database, I have created the model with the command: python manage.py inspectdb > /app/models/model.py. And it works wonderfully.
model.py
from __future__ import unicode_literals
from django.db import models
class Account(models.Model):
id = models.BigAutoField(primary_key=True)
login = models.CharField(unique=True, max_length=30)
lang = models.CharField(max_length=4)
forum_name = models.CharField(max_length=80)
hwid = models.CharField(max_length=255, blank=True, null=True)
use_allowed_hwids = models.IntegerField(blank=True, null=True)
password = models.CharField(max_length=45)
social_id = models.CharField(max_length=13)
email = models.CharField(max_length=64)
create_time = models.DateTimeField(blank=True, null=True)
status = models.CharField(max_length=8)
reason = models.CharField(max_length=255, blank=True, null=True)
securitycode = models.CharField(max_length=192, blank=True, null=True)
newsletter = models.IntegerField(blank=True, null=True)
empire = models.IntegerField()
name_checked = models.IntegerField()
availdt = models.DateTimeField(db_column='availDt', blank=True, null=True) # Field name made lowercase.
mileage = models.IntegerField()
cash = models.IntegerField()
gold_expire = models.DateTimeField()
silver_expire = models.DateTimeField()
safebox_expire = models.DateTimeField()
autoloot_expire = models.DateTimeField()
fish_mind_expire = models.DateTimeField()
marriage_fast_expire = models.DateTimeField()
money_drop_rate_expire = models.DateTimeField()
shop_double_up_expire = models.DateTimeField()
total_cash = models.IntegerField()
total_mileage = models.IntegerField()
ip = models.CharField(max_length=255, blank=True, null=True)
last_pw_change = models.DateTimeField(blank=True, null=True)
web_admin = models.IntegerField()
web_ip = models.CharField(max_length=15)
web_aktiviert = models.CharField(max_length=32)
real_name = models.CharField(max_length=32, blank=True, null=True)
question1 = models.CharField(max_length=64, blank=True, null=True)
answer1 = models.CharField(max_length=64, blank=True, null=True)
last_vote = models.DateTimeField(blank=True, null=True)
lostpw_token = models.CharField(max_length=64, blank=True, null=True)
last_play = models.DateTimeField(blank=True, null=True)
remember_token = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'account'
All works fine, but I can't see any "objects" attribute in my intellisense, and if i write it i can see only "Accounts.objects"... And no more
screenshot
I'm using Python 36
Related
I've got the following models:
class Match(models.Model):
objects = BulkUpdateOrCreateQuerySet.as_manager()
id = models.AutoField(primary_key=True)
betsapi_id = models.IntegerField(unique=True, null=False)
competition:Competition = models.ForeignKey(Competition, on_delete=models.CASCADE, related_name='matches')
season:Season = models.ForeignKey(Season, on_delete=models.CASCADE, related_name='season_matches', null=True, default=None)
home:Team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='home_matches')
away:Team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='away_matches')
minute = models.IntegerField(default=None, null=True)
period = models.CharField(max_length=25, default=None, null=True)
datetime = models.DateTimeField()
status = models.IntegerField()
opta_match = models.OneToOneField(OptaMatch, on_delete=models.CASCADE, related_name='match', default=None, null=True)
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
class Season(models.Model):
id = models.AutoField(primary_key=True)
competition:Competition = models.ForeignKey(to=Competition, on_delete=models.CASCADE, null=False, blank=False, related_name='seasons')
start_date = models.DateTimeField(blank=False, null=False)
end_date = models.DateTimeField(blank=False, null=False)
name = models.CharField(max_length=255, null=True, default=None)
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
class Event(models.Model):
objects = BulkUpdateOrCreateQuerySet.as_manager()
id = models.AutoField(primary_key=True)
betsapi_id = models.IntegerField(unique=True)
name = models.CharField(max_length=255)
minute = models.IntegerField()
player_name = models.CharField(max_length=255, null=True, default=None)
extra_player_name = models.CharField(max_length=255, null=True, default=None)
period = models.CharField(max_length=255)
team:Team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name='events')
match:Match = models.ForeignKey(Match, on_delete=models.CASCADE, related_name='events')
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
And I wrote the following annotations to a Match object:
all_matches_qs = (Match
.objects
.filter(status=1)
.select_related('home', 'away', 'competition', 'competition__country', 'season', 'opta_match')
.prefetch_related(
Prefetch("season", queryset=Season.objects.prefetch_related(
Prefetch("season_matches", queryset=Match.objects.prefetch_related(
Prefetch('statistics')))
)),
Prefetch('events', queryset=Event.objects.select_related("team", "match").filter(name__in=["Corner", "Goal", "Substitution", "Yellow Card", "Red Card"])),
)
.annotate(season_statistics_count=Count('season__season_matches__statistics'))
.annotate(goals=Count('events', distinct=True, filter=(Q(events__name='Goal') & Q(events__team=F('home')))))
)
Executing this on about 25 records takes me about 3.75 seconds whereas if I remove one of the annotations (doesn't matter which one) the time to execute drops to about 0.5 seconds.
Also, removing the filter(status=1) seems to be dropping the time to execute to near 0.3s.
I'm wondering what is causing this issue and how to ensure I can execute both annotations AND the filter without a drastic performance drop?
I have to serialize spare instance from spare variety many to many field.
Models.py
class SpareVariety(models.Model):
quality = models.ForeignKey(Quality, max_length=255, on_delete=models.CASCADE, null=True, blank=True)
variety_name = models.CharField(max_length=255, null=True, blank=True)
property = models.ForeignKey(SpareProperty, null=True, blank=True, on_delete=models.CASCADE)
purchase_price = models.PositiveIntegerField(help_text="in INR", blank=True, null=True)
retail_price = models.FloatField(help_text="All values in INR", blank=True, null=True)
dealer_price = models.FloatField(help_text="All values in INR", blank=True, null=True)
stock_available = models.PositiveIntegerField(blank=True, null=True,default=True)
spare_costing = models.ForeignKey(SpareCosting, on_delete=models.CASCADE, blank=True, null=True)
spare_discount = models.ForeignKey(Discount, on_delete=models.CASCADE, blank=True, null=True)
is_available = models.BooleanField(default=False)
date_added = models.DateTimeField(auto_now=True)
date_updated = models.DateTimeField(auto_now_add=True)
class Spare(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
spare_variety = models.ManyToManyField(SpareVariety, related_name='spare_varieties', null=True, blank=True)
name = models.CharField(max_length=255, help_text="Enter the name of spare (Ex:Display, Speakers)")
type = models.ForeignKey(Type, blank=True, null=True, on_delete=models.CASCADE)
date_added = models.DateTimeField(auto_now=True)
date_updated = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s - %s' % (self.product.name, self.name)
Serialize the spare model from spare variety serializer
serializers.py
class SpareVarietySerializer(serializers.HyperlinkedModelSerializer):
spare_costing= SpareCostingSerializer(many=False, read_only=False)
spare_discount = DiscountSerializer(many=False, read_only=False)
quality = QualitySerializer(many=False, read_only=False)
property = SparePropertySerializer(many=False, read_only=False)
spare_name = serializers.CharField(read_only=True, source="spare.name")
class Meta:
model = SpareVariety
fields = ['id','quality','variety_name','purchase_price','spare_name','retail_price','property', 'dealer_price', 'stock_available','spare_costing','spare_discount','is_available', 'date_added', 'date_updated',]
I have a problem in obtaining a single id from a queryset. I post my models and views in order to be more clear:
models.py
class MissionEntry(models.Model):
student = models.ForeignKey(
Student, on_delete=models.DO_NOTHING, blank=True, null=True)
mission = models.ForeignKey(
Mission, on_delete=models.DO_NOTHING, null=True, blank=True)
log_entry = models.ForeignKey(
LogEntry, on_delete=models.DO_NOTHING, blank=True, null=True)
learning_objective = models.ForeignKey(
LearningObjective, on_delete=models.DO_NOTHING, blank=True, null=True)
grade = models.CharField(
max_length=10, choices=GRADING_VALUE, blank=True, null=True)
note = models.TextField(blank=True, null=True)
debriefing = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return str(self.mission) + ' ' + str(self.log_entry)
class Meta:
verbose_name_plural = 'Mission Entries'
class MissionEntryStatus(models.Model):
mission = models.ForeignKey(
Mission, on_delete=models.PROTECT, null=True, blank=True)
student = models.ForeignKey(Student, on_delete=models.PROTECT)
is_completed = models.BooleanField(default=False)
is_failed = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class StudentMission(models.Model):
mission = models.ForeignKey(Mission, on_delete=models.PROTECT)
student_training_course = models.ForeignKey(
StudentTrainingCourse, on_delete=models.PROTECT)
mission_status = models.ForeignKey(
MissionEntryStatus, on_delete=models.PROTECT, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['mission__name']
def __str__(self):
return self.mission.name
class LogEntry(models.Model):
aircraft = models.ForeignKey(Aircraft, on_delete=models.DO_NOTHING)
adep = models.ForeignKey(
Aerodrome, on_delete=models.PROTECT, related_name='adep')
ades = models.ForeignKey(
Aerodrome, on_delete=models.PROTECT, related_name='ades')
date = models.DateField()
etd = models.TimeField()
ata = models.TimeField()
eet = models.TimeField()
function_type = models.ForeignKey(FunctionType, on_delete=models.PROTECT)
student = models.ForeignKey(
Student, on_delete=models.PROTECT, blank=True, null=True)
instructor = models.ForeignKey(
Instructor, on_delete=models.PROTECT, blank=True, null=True)
student_mission = models.ForeignKey(
'mission.StudentMission', on_delete=models.PROTECT, null=True, blank=True)
note = models.TextField(null=True, blank=True)
cross_country = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
views.py
def student_mission_list(request, pk):
student = Student.objects.get(id=pk)
student_training_course = StudentTrainingCourse.objects.filter(
student_id=student.id)
missions = StudentMission.objects.filter(
student_training_course_id__in=student_training_course)
mission_entry = MissionEntry.objects.filter(student_id=student)
log_entry = LogEntry.objects.filter(student_mission_id__in=missions)
print(log_entry)
context = {
'student': student,
'missions': missions,
'mission_entry': mission_entry,
}
return render(request, 'mission/student_mission_list.html', context)
In fact, what I need to do, is to obtain a single value for the log_entry. The problem is that, obviously, I am retrieving multiple values of log_entry. But I would like to get the single pk of the log_entry.
Any suggestion? Should I remodel the models.py file?
try this:
log_entry = LogEntry.objects.get(student_mission_id__in=missions)
print(log_entry.id)
I have these models that are linked with a foreign key
class Student(models.Model):
id_student = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=45)
last_name = models.CharField(max_length=45)
date_of_birth = models.DateField()
nationality = models.CharField(max_length=45)
gender = models.CharField(max_length=1)
street_name = models.CharField(max_length=45)
street_number = models.CharField(max_length=45)
postal_code = models.CharField(max_length=45)
city = models.CharField(max_length=45)
phone = models.CharField(max_length=45)
email = models.CharField(max_length=45)
study_id_study = models.ForeignKey('Study', models.DO_NOTHING, db_column='study_id_study')
start_year = models.CharField(max_length=45)
teacher_id_teacher = models.ForeignKey('Teacher', models.DO_NOTHING, db_column='teacher_id_teacher', related_name = 'tcr_id')
class Meta:
managed = False
db_table = 'Student'
class Teacher(models.Model):
id_teacher = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=45, blank=True, null=True)
last_name = models.CharField(max_length=45, blank=True, null=True)
date_of_birth = models.DateField(blank=True, null=True)
nationality = models.CharField(max_length=45, blank=True, null=True)
street_name = models.CharField(max_length=45, blank=True, null=True)
street_number = models.CharField(max_length=45, blank=True, null=True)
postal_code = models.CharField(max_length=45, blank=True, null=True)
city = models.CharField(max_length=45, blank=True, null=True)
phone = models.CharField(max_length=45, blank=True, null=True)
gender = models.CharField(max_length=1, blank=True, null=True)
salary = models.CharField(max_length=45, blank=True, null=True)
study_counselor = models.CharField(max_length=45, blank=True, null=True)
picture_url = models.CharField(max_length=45, blank=True, null=True)
class Meta:
managed = True
db_table = 'Teacher'
I wanted to get the name of the teacher depending on teacher_id_teacher that we have in Student model, and which is foreign key as well
seems easy but I have been trying to do this for two days with no success,
Thanks you so much :)
These are the relevant classes of my app. I want basically understand if the a certain user (form AuthUser) is linked to a business (from BusinessInformation) by looking at UserBusinessInformation. Thanks
class AuthUser(models.Model):
password = models.CharField(max_length=128)
last_login = models.DateTimeField(blank=True, null=True)
is_superuser = models.IntegerField()
username = models.CharField(unique=True, max_length=150)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.CharField(max_length=254)
is_staff = models.IntegerField()
is_active = models.IntegerField()
date_joined = models.DateTimeField()
class Meta:
managed = False
db_table = 'auth_user'
class BusinessInformation(models.Model):
business_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
lat = models.CharField(max_length=255)
lng = models.CharField(max_length=255)
formatted_address = models.CharField(max_length=255, blank=True, null=True)
locality = models.CharField(max_length=255, blank=True, null=True)
country = models.CharField(max_length=255, blank=True, null=True)
administrative_area_level_5 = models.CharField(max_length=255, blank=True, null=True)
administrative_area_level_4 = models.CharField(max_length=255, blank=True, null=True)
administrative_area_level_3 = models.CharField(max_length=255, blank=True, null=True)
administrative_area_level_2 = models.CharField(max_length=255, blank=True, null=True)
administrative_area_level_1 = models.CharField(max_length=255, blank=True, null=True)
postal_code = models.CharField(max_length=45, blank=True, null=True)
route = models.CharField(max_length=255, blank=True, null=True)
street_number = models.CharField(max_length=45, blank=True, null=True)
phone = models.CharField(max_length=255, blank=True, null=True)
phone2 = models.CharField(max_length=255, blank=True, null=True)
phone3 = models.CharField(max_length=255, blank=True, null=True)
email = models.CharField(max_length=255, blank=True, null=True)
email2 = models.CharField(max_length=255, blank=True, null=True)
email3 = models.CharField(max_length=255, blank=True, null=True)
website = models.CharField(max_length=255, blank=True, null=True)
facebook = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'business_information'
class UserBusinessInformation(models.Model):
user = models.ForeignKey(AuthUser, models.DO_NOTHING)
business = models.ForeignKey(BusinessInformation, models.DO_NOTHING)
class Meta:
managed = False
db_table = 'user_business_information'
When I try to access to UserBusinessInformation in my views, I do not manage neither using _set.
def school(request, schoolname):
school_searched = BusinessInformation.objects.get(name=schoolname)
user_linked = school_searched.userbusinessinformation_set.all()
I miss the many to many field:
class BusinessInformation(models.Model):
business_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
users = models.ManyToManyField(AuthUser,
through='UserBusinessInformation')
...
Then, in your view:
def school(request, schoolname):
school_searched = BusinessInformation.objects.get(name=schoolname)
user_linked = school_searched.users.all()
Quoting Extra fields on many-to-many relationships django docs:
For these situations, Django allows you to specify the model that will be used to govern the many-to-many relationship. You can then put extra fields on the intermediate model. The intermediate model is associated with the ManyToManyField using the through argument to point to the model that will act as an intermediary.
Let me finish with a little advice, it is true, 'These are the relevant classes of my app', but you can illustrate this sample with just few fields. Learn about How to create a Minimal, Complete, and Verifiable example
[if you want access a field that’s a ForeignKey, you’ll get the related model object just like]
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()`enter code here`
def __str__(self):
return self.name
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __str__(self):
return self.title
[you can access like that.... ]
b = Book.objects.get(id=50)
b.publisher
b.publisher.website