The idea is super simple. This is specific to my project, I want to add data to 2 objects of the same model on one Django admin page and have one save button.
My class:
class Data(models.Model):
issuer = models.ForeignKey(Issuers, on_delete=models.CASCADE)
issuer_field = models.ForeignKey(Fields, on_delete=models.CASCADE, null=True)
data_year = models.PositiveSmallIntegerField()
long_term_ar = models.IntegerField()
section_1 = models.IntegerField()
short_term_ar = models.IntegerField()
short_term_investments = models.IntegerField()
cash = models.IntegerField()
To illustrate:I want to have 2 of those on the same page
I am new to Django, and it will be super helpful if you will help me implement this idea
Take a look on "Inlinemodeladmin objects": https://docs.djangoproject.com/en/2.2/ref/contrib/admin/#inlinemodeladmin-objects
You will be able to add more than one record at the same time:
Try this approach. I am still learning Django too. However, I believe that you dont need to specify the save instance since it is already built in django.
models.py
class Data(models.Model):
issuer = models.ForeignKey(Issuers, on_delete=models.CASCADE)
issuer_field = models.ForeignKey(Fields, on_delete=models.CASCADE, null=True)
data_year = models.PositiveSmallIntegerField()
long_term_ar = models.IntegerField()
section_1 = models.IntegerField()
short_term_ar = models.IntegerField()
short_term_investments = models.IntegerField()
cash = models.IntegerField()
admin.py
from django.contrib import admin
from .models import Data
class DataAdmin(admin.ModelAdmin):
list_display = ('issuer', 'issuer_field')
admin.site.register(Data)
Related
Good afternoon,
I am really struggling with getting a sum using Annotate in DJango.
I am using User object and the following models:
class Depts(models.Model):
dept_name = models.CharField(max_length=55)
dept_description = models.CharField(max_length=255)
isBranch = models.BooleanField(default=False)
def __str__(self):
return "{}".format(self.dept_name)
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='profile')
title = models.CharField(max_length=75)
dept = models.ForeignKey(Depts, on_delete=models.CASCADE, related_name="dept", null=True)
class ActivityLog(models.Model):
activity_datetime = models.DateTimeField(default=timezone.now)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, related_name='activity_user')
activity_category = models.ForeignKey(ActivityCategory, on_delete=models.CASCADE, null=True, related_name='activity_cat')
activity_description = models.CharField(max_length=100, default="Misc Activity")
class ActivityCategory(models.Model):
activity_name = models.CharField(max_length=40)
activity_description = models.CharField(max_length=150)
pts = models.IntegerField()
def __str__(self):
return '%s' % (self.activity_name)
What I need to do is get a group of departments with aggregating the sum of the pts earned by all the users activitylogs.
So a user is part of department, they do activities, each activity is of a type activity_category and has associated points. How can I query using the ORM to get a sum of points for everyone in each department?
Thank you, I cannot seem to wrap my mind around it.
You annotate the departments with the sum:
from django.db.models import Sum
Depts.objects.annotate(
total_pts=Sum('dept__user__activity_user__activity_category__pts')
)
Note: The related_name=… parameter [Django-doc]
is the name of the relation in reverse, so from the Depts model to the UserProfile
model in this case. Therefore it (often) makes not much sense to name it the
same as the forward relation. You thus might want to consider renaming the dept relation to userprofiles.
After setting the related_name='userprofiles', the query is:
from django.db.models import Sum
Depts.objects.annotate(
total_pts=Sum('userprofiles__user__activity_user__activity_category__pts')
)
I am studying Django and SQL and I have proposed to create a social network in which I can add all the reactions of a publication and add it to a new field where they are all added.
from django.db import models
# Create your models here.
class Post(models.Model):
# User
user = models.OneToOneField(User, on_delete=models.CASCADE)
post = models.TextField(max_length=500000)
# Data
comments = models.IntegerField()
reactions = models.IntegerField()
## Reaction Data
like = models.IntegerField()
love = models.IntegerField()
wow = models.IntegerField()
sad = models.IntegerField()
angry = models.IntegerField()
That is the model of publications, there is a field called reactions, and that will be the sum of all the reactions that publication has acquired. How can I create that sum?
Such architecture will be a little wrong.
from project_name import settings
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=50)
text = models.TextField()
likes = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name='post_likes')
love = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name='post_loves')
# ... another reactions
created_date = models.DateTimeField(default=timezone.now)
settings.AUTH_USER_MODEL is your User
After that you will then be able to not only know the number of reactions, but also to identify users
To count users:
post = Post.objects.create(author=..., ...) # create post
queryset = post.likes # will return users id, who liked post
queryset.count() # will return number of users
I am building a REST-API that will be consumed by an Angular application - this is for my guitar company’s website. There is an Artist Profile page that display an artist’s name, a short bio and a list of the projects(bands) they’re associated with and the date-ranges they were active with them. Here is where things get complicated.
Any given project can be associated with more than one artist - i.e. I could have two guitar players from the same band. I was able to solve that association by creating a many-to-many field and it worked great…until I realized that I have artists who have been in the same band at different times.
I have tried many approaches so far. I wish I could list them, but I kinda lost track. But, the code below is the where I am at right now. I can indeed associate a band with multiple artists, but I can’t associate different date ranges to different artists in the same bands. Any guidance is much appreciated.
class projectDate(models.Model):
begin = models.DateField()
end = models.DateField()
def __str__(self):
string_date_range = self.begin.strftime("%d/%m/%y") + "-" + self.end.strftime("%d/%m/%y")
return string_date_range
class artistProfiles(models.Model):
artist_name = models.CharField(max_length=20)
artist_image = models.URLField()
description = models.TextField(max_length=500)
band_website = models.URLField()
def __str__(self):
return self.artist_name
class artistProjects(models.Model):
project_name = models.CharField(max_length=20)
dates = models.ManyToManyField(projectDate, related_name='date_span')
artists = models.ManyToManyField(artistProfiles, related_name='projects')
def __str__(self):
return self.project_name
class artistSocialMedia(models.Model):
facebook = models.URLField()
twitter = models.URLField()
instagram = models.URLField()
artist = models.ForeignKey(artistProfiles, related_name='social_media', on_delete=models.CASCADE)
def __str__(self):
return self.artist.artist_name
artistProjects and projectDate should not be a many-to-many relationship, since a projectDate is specific to a project and unlikely to be shared by many. You can instead make artistProjects a foreign key in projectDate so that a artistProjects can have many projectDates but not vice versa:
class projectDate(models.Model):
begin = models.DateField()
end = models.DateField()
project = models.ForeignKey(artistProjects, related_name='dates')
Note that your artistProjects represents just one project, so you should avoid giving it a plural name. Naming it artistProject will make your code more readable.
Not sure whether i can solve your problems or not. I am going to describe it in simple way so you can just adjust it with your models.
These is my advice. Hope it solve your problems.
Artist Profile
id (PK)
artist_name
artist_image
description
band_website
Artist Social Media
id (PK)
artist_profile_id (FK)(Artist Profile)
facebook
twitter
instagram
Artist Project
id (PK)
artist_band_project_id (FK)(Artis Band Project)
Artist Band Project
id (PK)
begin
end
Artist Band Project Member
id (PK)
artis_band_project_id (FK)(Artist Band Project)
artis_profile_id (FK)(Artist Profile)
Regards,
Meikelwis Wijaya
#blhsing Ended up being the closest of all answers, but it took a little more massaging to get the relationships and JSON structure I was looking for.
Here is what worked for the models:
from django.db import models
class artistProfile(models.Model):
artist_name = models.CharField(max_length=20)
artist_image = models.URLField()
description = models.TextField(max_length=500)
band_website = models.URLField()
def __str__(self):
return self.artist_name
class artistProject(models.Model):
project_name = models.CharField(max_length=20)
def __str__(self):
return self.project_name
class projectTenure(models.Model):
begin = models.DateField()
# blank and null are allowed here in case an artists is still with a given project
end = models.DateField(blank=True, null=True)
project = models.ForeignKey(artistProject, on_delete=models.CASCADE)
artist = models.ForeignKey(artistProfile, on_delete=models.CASCADE,
related_name='projects')
def __str__(self):
# TODO: find a way to return the related project and artist names
string_date_range = self.begin.strftime("%d/%m/%y") + "-"
return string_date_range
class artistSocialMedia(models.Model):
facebook = models.URLField()
twitter = models.URLField()
instagram = models.URLField()
artist = models.ForeignKey(artistProfile, related_name='social_media',
on_delete=models.CASCADE)
def __str__(self):
return self.artist.artist_name
And here is how I serialized it:
from rest_framework import serializers
from .models import (artistProfile, artistProject, projectTenure, artistSocialMedia)
class artistSocialMediaSerializer(serializers.ModelSerializer):
class Meta:
model = artistSocialMedia
fields = ('facebook', 'twitter', 'instagram')
class artistProjectSerializer(serializers.ModelSerializer):
class Meta:
model = artistProject
fields = ('project_name',)
class projectTenureSerializer(serializers.ModelSerializer):
project_name = serializers.CharField(source='project.project_name')
class Meta:
model = projectTenure
fields = ('project_name', 'begin', 'end')
class artistProfileSerializer(serializers.ModelSerializer):
projects = projectTenureSerializer(many=True, read_only=True)
social_media = artistSocialMediaSerializer(many=True, read_only=True)
class Meta:
model = artistProfile
fields = ('artist_name', 'artist_image', 'description',
'band_website', 'projects', 'social_media')
I have a Django app that works perfectly on google app engine, using the datastore via djangae. However, the admin site throws an error:
NotSupportedError at /admin/auth/user/5629499534213120/change/
Cross-join where filters are not supported on the Datastore
This error only occurs when trying to edit the default Django user model. Not sure why this is happening.
I have used the default Django user model. (this is an app dealing with donations for a nonprofit)
models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class FoodSplashUser(models.Model):
base_user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.TextField(null=True)
city = models.TextField(null=True)
state = models.CharField(max_length=4, null=True)
zip = models.CharField(max_length=10, null=True)
def __str__(self):
return str(self.base_user.username)
class Organization(models.Model):
base_user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.TextField(null=True)
city = models.TextField(null=True)
state = models.CharField(max_length=4, null=True)
zip = models.CharField(max_length=10, null=True)
description = models.TextField(null=True)
image_url = models.URLField(null=True)
def __str__(self):
return str(self.base_user.username)
class DonationRequest(models.Model):
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now=True)
request_type = models.TextField(null=True)
description = models.TextField(null=True)
def __str__(self):
return str(self.organization.base_user.username) + " " + self.request_type
class DonationPromise(models.Model):
user = models.ForeignKey(FoodSplashUser, on_delete=models.CASCADE)
donation_request = models.ForeignKey(DonationRequest, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now=True)
verified = models.BooleanField(default=False)
def __str__(self):
return str(self.user.base_user.username) + " " + str(self.donation_request)
This app goes with the default Django admin interface, but I decided to make the classes below for easy editing later.
admin.py:
from django.contrib import admin
from . import models
# Register your models here.
class FoodSplashUserAdmin(admin.ModelAdmin):
pass
class OrganizationAdmin(admin.ModelAdmin):
pass
class DonationRequestAdmin(admin.ModelAdmin):
pass
class DonationPromiseAdmin(admin.ModelAdmin):
pass
admin.site.register(models.FoodSplashUser, FoodSplashUserAdmin)
admin.site.register(models.Organization, OrganizationAdmin)
admin.site.register(models.DonationRequest, DonationPromiseAdmin)
admin.site.register(models.DonationPromise, DonationPromiseAdmin)
This may be a separate error, but :
admin.site.register(models.DonationRequest, DonationPromiseAdmin)
admin.site.register(models.DonationPromise, DonationPromiseAdmin)
Shouldn't that first one be: DonationRequestAdmin?
NotSupportedError indicates that your code performs an action that is not possible with App Engine Datastore. Not all the Django ORM features can be used in a non-relational database which Datastore is. You are trying to create an entity that has some relations, which causes the error. Probably it is a good idea to use Gauth for authentication and user management, as described in the Djangae docs.
this is my models.py
from django.db import models
# Create your models here.
class Leagues(models.Model):
LeagueName = models.CharField(max_length=200)
class Team(models.Model):
TeamName = models.CharField(max_length=200)
class LeagueTable(models.Model):
league = models.ForeignKey(Leagues)
team = models.CharField(max_length=200)
matches_played = models.IntegerField()
matches_won = models.IntegerField()
matches_drawn = models.IntegerField()
matches_lost = models.IntegerField()
points = models.IntegerField()
class FixtureTable(models.Model):
league = models.ForeignKey(Leagues)
fixture_date = models.DateField('Fixture Date')
team_one = models.CharField(max_length=200)
team_one_score = models.IntegerField()
team_two = models.CharField(max_length=200)
team_two_score = models.IntegerField()
in the "class FixtureTable", i want team_one and team_two to be linked to two differnt teams in "class Team" models. how to create multiple relations, or is it possible.
PS: this is purely a noob, with a little experience in programming, but no experience with either python or databases.
thankyou.
You can create as many ForeignKeys as you like to the same model. I suspect what's tripping you up is Django giving you an error saying that you need to specify a related name.
By default, Django creates an attribute on the opposite model of the form <model>_set. So in the following scenario:
class FixtureTable(models.Model):
team_one = models.ForeignKey(Team)
Django would add a related manager to Team as fixturetable_set. If you then did:
class FixtureTable(models.Model):
team_one = models.ForeignKey(Team)
team_two = models.ForeignKey(Team)
Django would attempt to add the same attribute twice and obviously fail. The fix is to specify a related_name, so each can have a unique related manager:
class FixtureTable(models.Model):
team_one = models.ForeignKey(Team, related_name='team_one_fixturetables')
team_two = models.ForeignKey(Team, related_name='team_two_fixturetables')
Then, everything will work fine. You can specify whatever you like for related_name as long as it's unique for the model.