Undefined join condition between parent/child tables - join

Here's a model for a basic todo app, in which a user can create a tasklist that contains tasks:
from flask.ext.sqlalchemy import SQLAlchemy
from allot import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String)
tasklists = db.relationship('Tasklist', backref='user', lazy='dynamic')
tasks = db.relationship('Task', backref='user', lazy='dynamic')
def __init__(self, username, email):
self.username = username
class Tasklist(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String)
user_id = db.Column(db.Integer, db.ForeignKey('User.id'))
tasks = db.relationship('Task', backref='tasklist', lazy='dynamic')
def __init__(self, title, description, user_id):
self.title = title
self.user_id = user_id
class Task(db.Model):
id = db.Column(db.Integer, primary_key=True)
title= db.Column(db.String)
user_id = db.Column(db.Integer, db.ForeignKey('User.id'))
tasklist_id = db.Column(db.Integer, db.ForeignKey('Tasklist.id'))
def __init__ (self, title, status, user_id):
self.title = title
self.user_id = user_id
self.tasklist_id = tasklist_id
It generates this error:
ArgumentError: Could not determine join condition between parent/child
tables on relationship User.tasklists. Specify a 'primaryjoin'
expression. If 'secondary' is present, 'secondaryjoin' is needed as
well.
Adding primaryjoin arguments in the relationships, like this:
tasklists = db.relationship('Tasklist', backref='user', primaryjoin='user.id==Tasklist.user_id', lazy='dynamic')
... returns:
InvalidRequestError: One or more mappers failed to initialize - can't
proceed with initialization of other mappers.
What's wrong with the model?
I searched for an explanation in the [relationship][1] doc and on SO but to no avail.

One possibility is that when you're referencing foreign keys your using the uppercase "User" when you actually need to use the lowercase tablename reference. Try this:
from flask.ext.sqlalchemy import SQLAlchemy
from allot import db
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String)
tasklists = db.relationship('Tasklist', backref='user', lazy='dynamic')
tasks = db.relationship('Task', backref='user', lazy='joined'))
def __init__(self, username, email):
self.username = username
class Tasklist(db.Model):
__tablename__ = 'tasklist'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
tasks = db.relationship('Task', backref='tasklist', lazy='dynamic')
def __init__(self, title, description, user_id):
self.title = title
self.user_id = user_id
class Task(db.Model):
__tablename__ = 'task'
id = db.Column(db.Integer, primary_key=True)
title= db.Column(db.String)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
tasklist_id = db.Column(db.Integer, db.ForeignKey('tasklist.id'))
def __init__ (self, title, status, user_id):
self.title = title
self.user_id = user_id
self.tasklist_id = tasklist_id

Related

Django Admin list display cannot access foreign key attribute getting 'NoneType' object has no attribute

I'm trying to display a related field in the admin section but I'm getting the following error:
'NoneType' object has no attribute 'ref_code'
models.py
class Orden(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
producto = models.ManyToManyField(
ProductosOrden)
medio_de_venta = models.ForeignKey(MediosVenta, null=True, blank=False, on_delete=models.SET_NULL)
fecha_venta = models.DateField(blank=False, null=False)
ordenado = models.BooleanField(default=False)
ref_code = models.CharField(max_length=20, blank=True, null=True)
promo = models.ForeignKey(Promos,null=True, blank=True, on_delete=CASCADE )
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
vendido_por = models.CharField(max_length=20, null=False, blank=False)
class Meta:
verbose_name_plural = "Ordenes"
ordering = ["-id"]
def __str__(self):
return self.user.username
class Cliente(models.Model):
nombre = models.CharField(max_length=60, null=False, blank=False)
email = models.EmailField(blank=False, null=False)
email_valido = models.BooleanField(null=True, blank= True, verbose_name='Email Válido')
telefono = models.CharField(
max_length=17, null=True, blank=True, verbose_name='Teléfono')
lead = models.ForeignKey(Leads, models.SET_NULL, null=True, blank=True)
activo = models.BooleanField(default=False)
orden = models.ForeignKey(Orden, null=True, blank=True, on_delete=CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
created_by = models.CharField(max_length=20, null=False, blank=False)
class Meta:
verbose_name_plural = "Clientes"
ordering = ["-id"]
def __str__(self):
return self.nombre
admin.py
class ClienteAdmin(admin.ModelAdmin):
list_display = ('id', 'nombre', 'activo', 'get_ref_code')
readonly_fields=('id',)
search_fields = ['nombre']
actions = ['activa','desactivar']
#admin.action(description='Desactivar')
def desactivar(self, request, queryset):
queryset.update(activo = False)
#admin.action(description='Activar')
def desactivar(self, request, queryset):
queryset.update(activo = True)
#admin.display(description= 'ref_code')
def get_ref_code(self, obj):
if not obj.orden.ref_code:
return None
else:
return obj.orden.ref_code
If I just use the return as:
return obj.orden
results are displayed with the name, but I want to display the ref_code results which is under the Orden model and it is not working.
correct if condition because orden column is nullable in Cliente model.
#admin.display(description= 'ref_code')
def get_ref_code(self, obj):
if not obj.orden_id:
return None
else:
return obj.orden.ref_code

Django Admin prepopulate user's email

I need some help to prepopulate user's email in my models. There has to be an easy way, but i think i am too tired to see the trick. Basically what i need, after the user has been logged in to admin, i need those fields like user, email and name from the User model to be already populated with the info of the current user.
Your help will be much appreciated!
Currently my models are like this:
class ParcareManager(models.Manager):
def active(self, *args, **kwargs):
return super(ParcareManager, self).filter(draft=False).filter(parking_on__lte=timezone.now())
class Parcare(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, default=1, on_delete=True)
email=models.EmailField(blank=True, null=True)
# email = models.EmailField(settings.AUTH_USER_MODEL)
parking_on = models.DateField(auto_now=False, auto_now_add=False,blank=True, null=True)
parking_off = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True) #plecarea din parcare
location =models.CharField(max_length=120, default="P1", blank=True, null=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False,blank=True, null=True)
timestamp=models.DateTimeField(auto_now=False, auto_now_add=True,blank=True, null=True)
venire = models.TimeField(default=datetime.time(9, 00),auto_now=False, auto_now_add=False )
plecare = models.TimeField(default=datetime.time(
18, 00), auto_now=False, auto_now_add=False)
objects = ParcareManager()
def __str__(self):
return self.email + " | " + self.location + " | " + str(self.parking_on) + " | " + str(self.parking_off)
class Meta:
verbose_name_plural = "parcare"
class Meta:
ordering = ["-timestamp", "-updated"]
My admin is like this:
from django.contrib import admin
# Register your models here.
from .models import Parcare
class ParcareModelAdmin(admin.ModelAdmin):
list_display = [ "email","user", "location",
"parking_on", "parking_off", "venire", "plecare"]
list_display_links = ["email", "user" ]
list_editable = [ "parking_off", "parking_on","venire", "plecare"]
list_filter = ["parking_on", "parking_off", "venire", "plecare"]
search_fields = ["location", "name"]
class Meta:
model = Parcare
def email(self, obj):
return obj.user.email
admin.site.register(Parcare, ParcareModelAdmin)
If this is the case, then you may try this:
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
if not obj:
user = request.user
form.base_fields['user'].initial = user
form.base_fields['email'].initial = user.email
return form
Here you are fetching the logged in user object from request and then initializing the values from that user.

grails hql left outer join

how is it possible to left outer join 2 tables?
class Person {
String firstName
String lastName
String gender
//static hasMany = [votes: Vote]
static mapping = {
version false
}
static constrains = {
}
}
class Vote {
Person voter;
Person subject;
static mapping = {
version false
}
static constraints = {
voter nullable: false
subject nullable: false
}
}
i need to get every person thats not subjected to a vote, for a specific person.
lets say person 1 votes for 3 out of 5 persons, i need the other 2 that he didnt vote for to show up for him.
How is the query supposed to be?
EDIT:
def personInstance1 = new Person(firstName: "Antonio", lastName: "Vivaldi", gender: "m")
def personInstance2 = new Person(firstName: "Dennis", lastName: "Rodman", gender: "m")
def personInstance3 = new Person(firstName: "Marc", lastName: "Oh", gender: "m")
def personInstance4 = new Person(firstName: "Gudrun", lastName: "Graublume", gender: "w")
def personInstance5 = new Person(firstName: "Hilde", lastName: "Feuerhorn", gender: "w")
def personInstance6 = new Person(firstName: "Mandy", lastName: "Muller", gender: "w")
personInstance1.save()
personInstance2.save()
personInstance3.save()
personInstance4.save()
personInstance5.save()
personInstance6.save()
def voteInstance1 = new Vote(voter: personInstance1, subject: personInstance2)
def voteInstance2 = new Vote(voter: personInstance1, subject: personInstance3)
def voteInstance3 = new Vote(voter: personInstance1, subject: personInstance4)
def voteInstance4 = new Vote(voter: personInstance1, subject: personInstance5)
def voteInstance5 = new Vote(voter: personInstance2, subject: personInstance1)
voteInstance1.save()
voteInstance2.save()
voteInstance3.save()
voteInstance4.save()
voteInstance5.save()
this is my grails bootstrap-file , Antonio and Dennis have voted, and each need to be presented a list of people they didnt vote for.
EDIT:
this way i seem to get a result for Dennis, since he only voted once,
but if i put v.voter_id = 1,
to get a result for Antonio, the result doubles according to how many votes he did.
SELECT first_name FROM vote as v
LEFT OUTER JOIN person as p
ON v.subject_id != p.id AND v.voter_id = 2
WHERE p.id IS NOT NULL
Try this:
SELECT * FROM Person P
WHERE NOT EXISTS(
SELECT 'Vote' FROM Vote V
WHERE V.subject = P
)
In this way you'll extract all Person without Vote
EDIT
In SQL you can retrieve a matrix in this way:
CREATE TABLE #person (nome varchar(30))
CREATE TABLE #vote (votante varchar(30), candidato varchar(30))
INSERT INTO #person values
('Antonio Vivaldi'),
('Dennis Rodman'),
('Marc Oh'),
('Gudrun Graublume'),
('Hilde Feuerhorn'),
('Mandy Muller')
INSERT INTO #vote values
('Antonio Vivaldi', 'Dennis Rodman'),
('Antonio Vivaldi', 'Marc Oh'),
('Antonio Vivaldi', 'Gudrun Graublume'),
('Antonio Vivaldi', 'Hilde Feuerhorn'),
('Dennis Rodman', 'Antonio Vivaldi')
SELECT *
FROM #person p
CROSS JOIN #person c
WHERE NOT EXISTS(
SELECT 'X'
FROM #vote v
WHERE v.votante = p.nome
AND v.candidato = c.nome
)
AND p.nome <> c.nome
ORDER BY p.nome

Cross-database join in Flask-SQLAlchemy

I'm trying to do a cross-database join in Flask-SQLAlchemy:
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = '...Master...'
app.config['SQLALCHEMY_BINDS'] = { 'Billing': '...Billing...' }
db = SQLAlchemy(app)
class Account(db.Model):
__tablename__ = 'Accounts'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(255))
class Setting(db.Model):
__tablename__ = 'Settings'
AccountId = db.Column(db.Integer, db.ForeignKey(Account.id), primary_key=True)
Enabled = db.Column(db.Boolean)
class BillingAccount(db.Model):
__tablename__ = 'Account'
__bind_key__ = 'Billing'
id = db.Column(db.Integer, primary_key=True)
AccountId = db.Column(db.Integer, db.ForeignKey(Account.id))
currency = db.Column(db.Integer)
class AccountSetting(db.Model):
__table__ = db.join(Account, AccountSetting)
id = db.column_property(Account.id, AccountSetting.AccountId)
username = Account.username
enabled = Setting.Enabled
class AccountSettingBilling(db.Model):
__table__ = db.join(Account, AccountSetting).join(BillingAccount)
id = db.column_property(Account.id, AccountSetting.AccountId, BillingAccount.AccountId)
username = Account.username
enabled = Setting.Enabled
currency = BillingAccount.currency
With this I can successfully query AccountSetting.query.all() but not AccountSettingBilling.query.all(), which fails with error 208 (MSSQL for 'object does not exist').
If I examine the generated SQL I can clearly see that it is doing a JOIN on Account.AccountId=Accounts.id when I'd expect to see some reference to Billing, e.g. Billing.Account.AccountId=Accounts.id.
Having followed Cross database join in sqlalchemy and http://pythonhosted.org/Flask-SQLAlchemy/binds.html it looks to me as though I've done things correctly. What gives?
You define an object db = SQLAlchemy(app) - it is Database1. You refer to it everywhere, but there is no reference to Database2. Also note code refers to columns for the join using 2 part identifiers:
Account . AccountId and Accounts . id
whereas you wish to have 3 part identifiers:
Billing . Account . AccountId and [Accounts] . Accounts . id
You are missing this property for db name from definition of each Class:
__table_args__ = {'schema': 'Accounts'}
__table_args__ = {'schema': 'Billing'}

Order Django admin change list column by output of __unicode()__ method

Here's part of my Django app's models.py:
class Person(models.Model):
birth_year = WideYear(null=True, blank=True)
birth_year_uncertain = models.BooleanField()
death_year = WideYear(null=True, blank=True)
death_year_uncertain = models.BooleanField()
flourit_year = WideYear(null=True, blank=True)
flourit_year_uncertain = models.BooleanField()
FLOURIT_CHOICES = (
(u'D', u'Birth and death dates'),
(u'F', u'Flourit date'),
)
use_flourit = models.CharField('Date(s) to use', max_length=2, choices=FLOURIT_CHOICES)
index_entries = models.ManyToManyField(IndexEntry, null=True, blank=True)
def __unicode__(self):
if self.personname_set.filter(default_name__exact=True):
name = z(self.personname_set.filter(default_name__exact=True)[0])
else:
name = u'[Unnamed person]'
if self.use_flourit == u'D':
dates = '%s - %s' % (z(self.birth_year), z(self.death_year))
else:
dates = 'fl. ' + z(self.flourit_year)
return '%s (%s)' % (name, dates)
class PersonName(models.Model):
titles = models.CharField(max_length=65535, null=True, blank=True)
surname = models.CharField(max_length=255, null=True, blank=True)
first_name = models.CharField(max_length=255, null=True, blank=True)
middle_names = models.CharField(max_length=255, null=True, blank=True)
post_nominals = models.CharField(max_length=65535, null=True, blank=True)
default_name = models.BooleanField()
person = models.ForeignKey(Person, null=True, blank=True)
def __unicode__(self):
return '%s, %s %s' % (self.surname, self.first_name, self.middle_names)
class Meta:
unique_together = ("titles", "surname", "first_name", "middle_names", "post_nominals", "person")
unique_together = ("default_name", "person")
and here are the corresponding parts of my app's admin.py:
from reversion.admin import VersionAdmin
class PersonNameInline(admin.TabularInline):
model = PersonName
extra = 1
class PersonAdmin(VersionAdmin):
radio_fields = {"use_flourit": admin.HORIZONTAL}
inlines = [PersonNameInline]
admin.site.register(Person, PersonAdmin)
In the admin, this produces a change list as follows:
(source: sampablokuper.com)
As you can see, although the change list populates each row of the Person column with the output of the __unicode()__ method of the Person class, it does not order the rows of that column by the __unicode()__ method of the Person class.
How can I make it do so?
Many thanks in advance!
Django ordering is done in the database level. Unless you store the result of your unicode function in the DB, django is not going to be able to natively return results ordered in that fashion.
Storing an ordering value in the DB is probably the most expedient way to solve this problem.
Just hit the same problem, and I found the following link useful. Instead of sorting by unicode, it tries to sort by multiple columns, which may help solve the problem:
http://djangosnippets.org/snippets/2110/
Following Paul McMillan's suggestion, I added the following line to the class definition of Person:
ordering_string = models.CharField(max_length=255, null=True, blank=True)
I also put the this above the PersonName definition in models.py:
def post_save_person_and_person_name(sender, **kwargs):
person_name = kwargs['instance']
person = person_name.person
if person.ordering_string != unicode(person)[0:254]:
person.ordering_string = unicode(person)[0:254]
super(Person, person).save()
and put this below the PersonName definition in models.py:
post_save.connect(post_save_person_and_person_name, sender=PersonName)
So far, so good.
I think I might be able to improve on it by replacing the save() call above with a queryset update() . I'd welcome suggestions on that front!

Resources