@ldsad7, as for us, we refused this feature and replaced it with regular microservices.
Hello. The code has been working well but today I got unexpected error of 'SpanningForeignKey' object has no attribute 'rel'. I looked at the ForeignKey code and it seems like the "rel" attribute has been changed to "remote_field"? And res.to seems to be changed to remote_field.model? Do you need to update the code?
Thanks, Sijian
**If you are using mysql and django2.2, kindly use the following for the ForeignKey **
**PROBE THANKS TO Jared Scott aka gcko **
from django.core import exceptions
from django.db.models.fields.related import ForeignKey
from django.db.utils import ConnectionHandler, ConnectionRouter
#from django.db.models import ForeignKey
connection = ConnectionHandler()
router = ConnectionRouter()
class SpanningForeignKey(ForeignKey):
def validate(self, value, model_instance):
if self.remote_field.parent_link:
return
# Call the grandparent rather than the parent to skip validation
super(ForeignKey, self).validate(value, model_instance)
if value is None:
return
using = router.db_for_read(self.remote_field.model, instance=model_instance)
qs = self.remote_field.model._default_manager.using(using).filter(
**{self.remote_field.field_name: value}
)
qs = qs.complex_filter(self.get_limit_choices_to())
if not qs.exists():
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={
'model': self.remote_field.model._meta.verbose_name,
'pk': value,
'field': self.remote_field.field_name, 'value': value,
}, # 'pk' is included for backwards compatibility
)
Hello,
Thanks you very much for your solution! However, it seems like migrations are not registering the SpanningForeignKey field. Adding them manually does not fix the issue aswell. Do you have any idea what is going wrong?
Regards,
Charles
@Sarrus1, whats the problem
is there any solution for manytomanyfield?
@Sarrus1, cut and paste the error for further help..@diegobill, have not seen one,but keep on looking, the internet is a strange place
I'm trying to use this class to allow for a lookup of a user in auth_user in a 'default' DB , different from my application DB. ('test2' here).
the table I created looks like:
CREATE TABLE IF NOT EXISTS "test2_m1" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "description" text NOT NULL, "user_id" integer NOT NULL ); -- no foreign key
But when I run these statements, Django generates the following query wth an INNER JOIN to auth_user, and SpanningForeignKey.validate() is not called at all (I added a debug line to test):
from test2.models import *
M1.objects.get(user__username = 'xyz')
db_for_read: test2 ---> test2
ZZZZ SELECT "test2_m1"."id", "test2_m1"."user_id", "test2_m1"."description" FROM "test2_m1" INNER JOIN "auth_user" ON ("test2_m1"."user_id" = "auth_user"."id") WHERE "auth_user"."username" = xyz
The problem here is that the underlying Django code still wants to form a query with an INNER JOIN on the app-specific DB, not the default DB, expecting "auth_user" to be in that DB.
My questions:
- How do I get Django to NOT do that INNER JOIN on "auth_user" , especially when I specified no foreign key in the app's DB ? Shouldn't the SpanningForeignKey class prevent this from happening ?
- If SpanningForeignKey.validate() won't do that for me, what other method(s) must I override ?
Also searching for an answer as the proposed solution doesn't work for me (django 2.2).