SQLAlchemy: get Model from table name. This may imply appending some function to a metaclass constructor as far as I can see

Inspired by Eevee‘s comment: def get_class_by_tablename(tablename): “””Return class reference mapped to table. :param tablename: String with name of table. :return: Class reference or None. “”” for c in Base._decl_class_registry.values(): if hasattr(c, ‘__tablename__’) and c.__tablename__ == tablename: return c

ValueError – Cannot assign: must be an instance

You don’t need to pass the department id, the instance itself is enough. The following should work just fine: new_team = Team( nickname = team_name, employee_id = employee_id, department_id = Department.objects.get(password = password, department_name = department_name)) Just a note: don’t ever name your foreign fields something_id. That something is enough. Django is meant to make … Read more

Django model with 2 foreign keys from the same table

I haven’t done this yet, but I used inspectdb to generate the models.py file from an existing DB that does exactly that – this is what inspectdb threw back, so it should work: creator = models.ForeignKey(Users, null=True, related_name=”creator”) assignee = models.ForeignKey(Users, null=True, related_name=”assignee”) Hope that works for you – if it doesn’t I am going … Read more

MongoDB normalization, foreign key and joining

MongoDB doesn’t support server side foreign key relationships, normalization is also discouraged. You should embed your child object within parent objects if possible, this will increase performance and make foreign keys totally unnecessary. That said it is not always possible, so there is a special construct called DBRef which allows to reference objects in a … Read more