QString QDjangoCompiler::databaseColumn(const QString &name) { QDjangoMetaModel model = baseModel; QString modelPath; QString modelRef = referenceModel(QString(), &model); QStringList bits = name.split(QLatin1String("__")); while (bits.size() > 1) { const QByteArray fk = bits.first().toLatin1(); QDjangoMetaModel foreignModel; if (!model.foreignFields().contains(fk)) { // this might be a reverse relation, so look for the model // and if it exists continue foreignModel = QDjango::metaModel(fk); if (!foreignModel.isValid()) break; reverseModelRefs[bits.first()] = foreignModel.primaryKey(); } else { foreignModel = QDjango::metaModel(model.foreignFields()[fk]); } // store reference if (!modelPath.isEmpty()) modelPath += QLatin1String("__"); modelPath += bits.first(); modelRef = referenceModel(modelPath, &foreignModel); model = foreignModel; bits.takeFirst(); } const QDjangoMetaField field = model.localField(bits.join(QLatin1String("__")).toLatin1()); return modelRef + QLatin1Char('.') + driver->escapeIdentifier(field.column(), QSqlDriver::FieldName); }
QString QDjangoCompiler::databaseColumn(const QString &name) { QDjangoMetaModel model = baseModel; QString modelName; QString modelPath; QString modelRef = referenceModel(QString(), &model, false); QStringList bits = name.split(QLatin1String("__")); while (bits.size() > 1) { const QByteArray fk = bits.first().toLatin1(); QDjangoMetaModel foreignModel; bool foreignNullable = false; if (!modelPath.isEmpty()) modelPath += QLatin1String("__"); modelPath += bits.first(); if (!model.foreignFields().contains(fk)) { // this might be a reverse relation, so look for the model // and if it exists continue foreignModel = QDjango::metaModel(fk); QDjangoReverseReference rev; const QMap<QByteArray, QByteArray> foreignFields = foreignModel.foreignFields(); foreach (const QByteArray &foreignKey, foreignFields.keys()) { if (foreignFields[foreignKey] == baseModel.className()) { rev.leftHandKey = foreignModel.localField(foreignKey + "_id").column(); break; } } if (rev.leftHandKey.isEmpty()) { qWarning() << "Invalid field lookup" << name; return QString(); } rev.rightHandKey = foreignModel.primaryKey(); reverseModelRefs[modelPath] = rev; } else {
/** Sets the primary key for this QDjangoModel. * * \param pk */ void QDjangoModel::setPk(const QVariant &pk) { const QDjangoMetaModel metaModel = QDjango::metaModel(metaObject()->className()); setProperty(metaModel.primaryKey(), pk); }
/** Returns a string representation of the model instance. */ QString QDjangoModel::toString() const { const QDjangoMetaModel metaModel = QDjango::metaModel(metaObject()->className()); const QByteArray pkName = metaModel.primaryKey(); return QString::fromLatin1("%1(%2=%3)").arg(QString::fromLatin1(metaObject()->className()), QString::fromLatin1(pkName), property(pkName).toString()); }
/** Returns the primary key for this QDjangoModel. */ QVariant QDjangoModel::pk() const { const QDjangoMetaModel metaModel = QDjango::metaModel(metaObject()->className()); return property(metaModel.primaryKey()); }