assign list elements to database entity in sqlalchemy from a list - sqlalchemy

I have a simple model as follows, i have a list of games and i want all the players to assign one and only one game, from the list of games. How would i do it in sqlalchemy.
As of now i am using flask sqlalchemy, but the question does not limit to flask-sqlalchemy.
games = ['soccer', 'hockey', 'baseball', 'cricket', 'basketball']
from flask.ext.sqlalchemy import SQLAlchemy
class Gamer(db.Model):
id = db.Column(db.Integer, primary_key=True)
team_name = db.Column(db.String(80))
game_name = db.Column(db.String(80))
game = db.Column(db.String(80), i want only games from the list of games)
The analogous django functionality is found here.

First of all, the django functionality you refer to is not really analogous to the model you list. The reason for this is that Gamer is a model whereas SimpleForm is a UI form.
However, below are few ways you can validate the model:
1) Create a separate tabbe for Games, add the values as rows, and create a N-to-1 relationship from Gamer to the Game. This is readable, and will validate your data on both the business and DB levels
class Game(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
class Gamer(db.Model):
# ...
game_id = db.Column(db.Integer, ForeignKey('game.id'))
game = relationship(Game)
2) Create and use a simple validator
from sqlalchemy.orm import validates
class Gamer(db.Model):
# ...
#validates('game')
def validate_game(self, key, address):
assert address in games
return address
edit-1: alternatively, take a look at the The Enum Recipe. But again this is on a model level, not UI validation.

Related

Django: Can you create a relationship with an auto-generated through table?

My code looks something like this:
from django.db import models
from django.conf import settings
User = settings.AUTH_USER_MODEL
class Dish(models.Model):
name = models.CharField(max_length=200)
class Meal(models.Model):
name = models.CharField(max_length=200)
dishes = models.ManyToManyField(Dish)
The many-to-many dishes field will result in a database table called 'myapp_meal_dishes' being created that includes id, meal_id, and dish_id fields.
I would like to add a MealDishEater model that connects with that auto-generated table:
class MealDishEater(models.Model):
meal_dish = models.ForeignKey(MealDishes, on_delete=models.CASCADE)
eater = models.ForeignKey(User, on_delete=models.PROTECT)
ate_meal = models.BooleanField(default=False)
Of course, that doesn't work, because MealDishes is not defined. Is there a way to do this or do I have to create my own through table?
You can access the ManyToManyField intermediate model with the through attribute. So this should work:
class MealDishEater(models.Model):
meal_dish = models.ForeignKey(Meal.dishes.through, on_delete=models.CASCADE)
Personally, though, I always create explicit through models for ManyToManyFields rather than allowing such magic.

Django REST - Resolving Foreign Keys and M2M's in a Serializer

I have the following Models:
class Player(models.Model):
name = models.CharField(max_length=30)
class Achievement(models.Model):
name = models.CharField(max_length=30)
class UnlockedAchievement(models.Model):
achievement = models.ForeignKey(Achievement)
date = models.DateTimeField()
class PlayerAchievements(models.Model):
player = models.ForeignKey(Player)
unlocked_achievements = models.ManyToManyField(UnlockedAchievement, related_name="unlocked_achievements", blank=True, null=True)
With a PUT, I'm trying to resolve both the Player's foreign key as well as the nested relationship of all the Achievements. My JSON data effectively looks like this:
{"name":"playername",
"achievements":
{
"ach1":"timestamp",
"ach2":"timestamp",
}
}
What I can't figure out is the magic combination of which kinds of Serializers to use and, when using them, which serializer fields or nested Serializers to use to be able to resolve Players by name, and the unlocked achievements (and then their Achievement foreign keys) by providing a name.
In this case I don't have access to id numbers, hence why things are done by names.
Such a strange mixture it seems. Can anyone lend a hand? Thanks in advance!
You can use nested relationships to fully include the serialization of a related model:
class AchievementSerializer(serializers.ModelSerializer):
class Meta:
model = Achievement
class UnlockedAchievementSerializer(serializers.ModelSerializer):
achievement = AchievementSerializer(many=False)
class Meta:
model = UnlockedAchievement
class PlayerAchievementsSerializer(serializers.ModelSerializer):
unlocked_achievements = UnlockedAchievementSerializer(many=True)
class Meta:
model = PlayerAchievements
class PlayerSerializer(serializers.ModelSerializer):
player_achievements = PlayerAchievementsSerializer(many=False)
class Meta:
model = Player
Then just filter the Player object by name and serialize it.

How to build backref with both associatition object and secondaryjoin?

I need some models for instance following:
Work - e.g. works of literature.
Worker - e.g. composer, translator or something similar has contribution to work.
Thus, a 'type' field is required to distinguish workers by division of work. As SQLAlchemy's documentation, this case can benifit from association object like following:
class Work(base):
id = Column(Integer, primary_key=True)
name = Column(String(50))
description = Column(Text)
class Worker(base):
id = Column(Integer, primary_key=True)
name = Column(String(50))
description = Column(Text)
class Assignment(base):
work_id = Column(Integer, Foreignkey('work.id'), primary_key=True)
worker_id = Column(Integer, Foreignkey('worker.id'), primary_key=True)
type = Column(SmallInteger, nullable=True)
Nonetheless, how to take advantage of backref and alternatvie join condition for building relation immediately to implement that each Work object can retrieve and modify corresponding Worker(s) via different attributions for distinction. For example:
work = session.query(Work).get(1)
work.name
>>> 'A Dream of The Red Mansions'
work.composers
>>> [<Worker('Xueqin Cao')>]
work.translators
>>> [<Worker('Xianyi Yang')>, <Worker('Naidie Dai')>]
Vice versa:
worker = session.query(Worker).get(1)
worker.name
>>> 'Xueqin Cao'
worker.composed
>>> [<Work('A Dream of The Red Mansions')>]
worker.translated
>>> []
Adding secondaryjoin directly without secondary specified seems not feasible, besides, SQLAlchemy's docs notes that:
When using the association object pattern, it is advisable that the association-mapped table not be used as the secondary argument on a relationship() elsewhere, unless that relationship() contains the option viewonly=True. SQLAlchemy otherwise may attempt to emit redundant INSERT and DELETE statements on the same table, if similar state is detected on the related attribute as well as the associated object.
Then, is there some way to build these relations elegantly and readily ?
There's three general ways to go here.
One is, do a "vanilla" setup where you have "work"/"workers" set up without distinguishing on "type" - then, use relationship() for "composer", "composed", "translator", "translated" by using "secondary" to Assignment.__table__ along with custom join conditions, as well as viewonly=True. So you'd do writes via the vanilla properties only. A disadvantage here is that there's no immediate synchronization between the "vanilla" and "specific" collections.
Another is, same with the "vanilla" setup, but just use plain Python descriptors to give "composer", "composed", "translator", "translated" views in memory, that is, [obj.worker for obj in self.workers if obj.type == 'composer']. This is the simplest way to go. Whatever you put in the "vanilla" collections shows right up in the "filtered" collection, the SQL is simple, and there's fewer SELECT statements in play (one per Worker/Work instead of N per Worker/Work).
Finally, the approach that's closest to what you're asking, with primary joins and backrefs, but note with the association object, the backrefs are between Work/Assignment and Assignment/Worker, but not between Work/Worker directly. This approach probably winds up using more SQL to get at the results but is the most complete, and also has the nifty feature that the "type" is written automatically. We're also using a "one way backref", as Assignment doesn't have a simple way of relating back outwards (there's ways to do it but it would be tedious). Using a Python function to automate creation of the relationships reduces the boilerplate, and note here I'm using a string for "type", this can be an integer if you add more arguments to the system:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
Base = declarative_base()
def _work_assignment(name):
assign_ = relationship("Assignment",
primaryjoin="and_(Assignment.work_id==Work.id, "
"Assignment.type=='%s')" % name,
back_populates="work", cascade="all, delete-orphan")
assoc = association_proxy("%s_assign" % name, "worker",
creator=lambda worker: Assignment(worker=worker, type=name))
return assoc, assign_
def _worker_assignment(name):
assign_ = relationship("Assignment",
primaryjoin="and_(Assignment.worker_id==Worker.id, "
"Assignment.type=='%s')" % name,
back_populates="worker", cascade="all, delete-orphan")
assoc = association_proxy("%s_assign" % name, "work",
creator=lambda work: Assignment(work=work, type=name))
return assoc, assign_
class Work(Base):
__tablename__ = 'work'
id = Column(Integer, primary_key=True)
name = Column(String(50))
description = Column(Text)
composers, composer_assign = _work_assignment("composer")
translators, translator_assign = _work_assignment("translator")
class Worker(Base):
__tablename__ = 'worker'
id = Column(Integer, primary_key=True)
name = Column(String(50))
description = Column(Text)
composed, composer_assign = _worker_assignment("composer")
translated, translator_assign = _worker_assignment("translator")
class Assignment(Base):
__tablename__ = 'assignment'
work_id = Column(Integer, ForeignKey('work.id'), primary_key=True)
worker_id = Column(Integer, ForeignKey('worker.id'), primary_key=True)
type = Column(String, nullable=False)
worker = relationship("Worker")
work = relationship("Work")
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
session = Session(e)
ww1, ww2, ww3 = Worker(name='Xueqin Cao'), Worker(name='Xianyi Yang'), Worker(name='Naidie Dai')
w1 = Work(name='A Dream of The Red Mansions')
w1.composers.append(ww1)
w1.translators.extend([ww2, ww3])
session.add(w1)
session.commit()
work = session.query(Work).get(1)
assert work.name == 'A Dream of The Red Mansions'
assert work.composers == [ww1]
assert work.translators == [ww2, ww3]
worker = session.query(Worker).get(ww1.id)
assert worker.name == 'Xueqin Cao'
assert worker.composed == [work]
assert worker.translated == []
worker.composed[:] = []
# either do this...
session.expire(work, ['composer_assign'])
# or this....basically need composer_assign to reload
# session.commit()
assert work.composers == []

Programmatically identify django foreignkey links

Similar to the question I asked here, if I wanted to list all of the foreign key relationships from a model, is there a way to detect these relationships (forward and backward) automatically?
Specifically, if Model 1 reads
class Mdl_one(models.Model):
name = models.CharField(max_length=30)
and Model 2 reads
class Mdl_two(models.Model):
mdl_one = models.ForeignKey(Mdl_one)
name = models.CharField(max_length=30)
Is there some meta command I can run from Mdl_one (like Model_one()._meta.one_to_many) that tells me that mdl_two has a one-to-many foreign key relationship with it? Simply that mdl_one and mdl_two can be connected, not necessarily that any two objects actually are?
This is you are looking for:
yourModel._meta.get_all_related_objects()
Sample (Edited):
class Alumne(models.Model):
id_alumne = models.AutoField(primary_key=True)
grup = models.ForeignKey(Grup, db_column='id_grup')
nom_alumne = models.CharField("Nom",max_length=240)
cognom1alumne = models.CharField("Cognom1",max_length=240)
cognom2alumne = models.CharField("Cognom2",max_length=240, blank=True)
...
class Expulsio(models.Model): <---!
alumne = models.ForeignKey(Alumne, db_column='id_alumne')
dia_expulsio = models.DateField(blank=True)
...
>>> from alumnes.models import Alumne as A
>>> for x in A._meta.get_all_related_objects():
... print x.name
...
horaris:alumneexclosdelhorari
presencia:controlassitencia
incidencies:entrevista
incidencies:expulsio <---!
incidencies:incidencia
incidencies:incidenciadaula
seguimentTutorial:seguimenttutorial

Django - How to link to a legacy database via intermediary?

I have to integrate a legacy design with my Django project and I am looking for some advice on using an intermediary. The existing design works but now I need to filter the Project by a third table.
In english - I have a Organization (Django) and which points to many Projects (Legacy). But all of the Project don't refer to that Organization. I have a third table ProjectMap which was build via a Trigger to address that. It corresponds the Organization.name to a project.
How do I glue this together in order allow me to do this.
projects = Organization.objects.get(pk=1).projects.all()
And it won't get ALL of the projects just the ones which match in the third table. Here is what I have so far..
By the way if anyone has a better strategy I'm all ears
class Organization(models.Model):
name = models.CharField(max_length=32)
projects = models.ManyToManyField(Project)
class Project(models.Model):
"""This is the project info page..
Note: 'id' does exist and is the pk.
"""
result_number = models.IntegerField(null=True, db_column='LBLDGRUNNO', blank=True)
building_number = models.IntegerField(db_column='LBLDGNO')
name = models.CharField(max_length=150, db_column='SPIBLGNAME', blank=True)
class Meta:
db_table = u'PROJINFO'
managed = False
class ProjectMap(models.Model):
projinfo_table_id = models.IntegerField(null=True) # 'id' of Project
name = models.CharField(max_length=128, null=True) # 'name' in Organization
Thanks so much!
Not sure if this is what your asking, but you can use the through call on the ManyToManyField to define an intermediate table:
class Organization(models.Model):
name = models.CharField(max_length=32)
projects = models.ManyToManyField(Project, through="ProjectOrganisation")
class Project(models.Model):
#Stuff Here
class ProjectOrganisation(models.Model):
project = models.ForeignKey(Project)
organization = models.ForeignKey(Organization)
#Other Fields Here
Django does this automatically with manytomany fields anyway, just if you want to add extra fields, this is the way to do it.