Is it possible to do something like this?
class Doc1:
fieldd1 = StringField()
class Doc2:
fieldd2 = ReferenceField(Doc1.fieldd1)
Or should I just reference the Doc and then get the field information whenever I need it
This not posible and it is reference to document. To get fieldd1 you must do:
class Doc1(Document):
fieldd1 = StringField()
class Doc2(Document):
fieldd2 = ReferenceField(Doc1)
Doc2.objects.first().fieldd2.fieldd1
If you want just include document to another as part of one document then look at EmbeddedDocument and EmbeddedDcoumentField:
class Doc1(EmbeddedDocument):
fieldd1 = StringField()
class Doc2(Document):
fieldd2 = EmbeddedDcoumentField(Doc1)
Doc2.objects.first().fieldd2.fieldd1
But you always can set own properties:
class Doc1(Document):
fieldd1 = StringField()
class Doc2(Document):
fieldd2 = ReferenceField(Doc1)
#property
def fieldd1(self):
return self.fieldd2.fieldd1
Doc2.objects.first().fieldd1
See documentation: https://mongoengine-odm.readthedocs.org/en/latest/guide/defining-documents.html.
Related
i need to join a table without a direct relationship.
models.py:
class FooModel():
bar = ForeignKey(Bar)
class BarModel():
pass
class BazModel():
bar = ForeignKey(Bar)
class QuxModel():
foo = ForeignKey(Foo)
tried to reach Foo from Baz but didn't work
viewset.py:
def BazView(viewsets.ModelViewSet):
queryset = model.BazModel.objects.all().prefetch_related('bar').prefetch_related('baz__bar')
serializer_class = serializer.Baz
def get_queryset(self):
return self.queryset
serializer.py
class FooSerializer(serializer.ModelSerializer):
class Meta:
model = FooModel
exlude = []
class BarSerializer(serializer.ModelSerializer):
class Meta:
model = BarModel
exlude = []
class BazSerializer(serializer.ModelSerializer):
foo = FooSerializer()
class Meta:
model = BarModel
exlude = []
class QuxSerializer(serializer.ModelSerializer):
class Meta:
model = QuxModel
exlude = []
using prefetch like that i got an error saying that Baz has no foo field.
also would like to get data from QuxModel based on Foo FK...
how could i perform this?
Try this:
model.BazModel.objects.all().select_related('bar').prefetch_related('bar__foomodel_set')
And then in BazSerializer, you can set the source for foo like this:
class BazSerializer(serializer.ModelSerializer):
foo = FooSerializer(source='bar.foomodel_set')
This will tell the foo serializer to get it's data from the FooModel objects using BazModel's bar.
You might have to change foomodel_set to the related name you have set to access FooModel from BarModel.
I'm trying to add a model class with abstract=True in meta to another abstract class.For example,
class MainRecord(models.Model):
date = models.DateTimeField(auto_now_add=True,null=True)
X_data = models.EmbeddedField(
model_container=X,
model_form_class= X_Form,
)
class X(models.Model):
HRCT = models.BooleanField()
Y = models.EmbeddedField(
model_container=Y,
model_form_class=Y_Form,
)
class Meta:
abstract = True
class Y(models.Model):
Y_present = models.BooleanField()
Location = models.EmbeddedField(
model_container=Location,
model_form_class=Location_Form,
)
class Meta:
abstract = True
In Django admin, I'm able to see the embedded fields and it's corresponding checkboxes. I'm able to add/update values for the boolean field HRCT (Embedded from class X) but I'm not able to save the values/updates in the fields embedded from class Y to class X.
Could someone translate this Java Pseudo code with generics to Django models? I don't understand the content type concept. It would also be possible to leave out the map and just have a list of KeyValuePairs or KeyValueExamples.
class Dictionary<T extends KeyValuePair>
class KeyValuePair
String key
String value
class KeyValueExample extends KeyValuePair
String example
class Container
Dictionary<KeyValuePair> itemsOne
Dictionary<KeyValueExample> itemsTwo
Django's contenttypes doesn't have anything common with generics from Java. Python has a dynamic type system so there is no need for generics.
This means that you can put any object of any class into the dictionary:
class Container(object):
def __init__(self):
self.itemsOne = {}
self.itemsTwo = {}
container = Container()
container.itemsOne['123'] = '123'
container.itemsOne[321] = 321
container.itemsTwo[(1,2,3)] = "tuple can be a key"
If you want to implement your classes in django models then code could be something like this:
class KeyValuePairBase(models.Model):
key = models.CharField(max_length=30)
value = models.CharField(max_length=30)
class Meta:
abstract = True
class KeyValuePair(KeyValuePairBase):
pass
class KeyValueExample(KeyValuePairBase):
example = models.CharField(max_length=30)
class Container(models.Model):
items_one = models.ManyToManyField(KeyValuePair)
items_two = models.ManyToManyField(KeyValueExample)
# usage of these models
kvp = KeyValuePair.objects.create(key='key', value='value')
kve = KeyValueExample.objects.create(key='key', value='value',
example='Example text')
container = Container.objects.create()
container.items_one.add(kvp)
container.items_two.add(kve)
I am trying to use google's ndb model, adding some auto fields and definitions prior to model definition. The below code works well. My question is, though, any specific ndb model implementation is not used ( given I will be destroyed, if google changes anything) do you see any issue with portability of below
class MetaModel(type):
def __new__(cls,name,bases,attrs):
super_new = super(MetaModel,cls).__new__
if name == "Model":
return super_new(cls,name,bases,attrs)
if attrs.get('auto_date_time',True):
attrs['date_add'] = ndb.DateTimeProperty(auto_now_add= True)
attrs['date_upd'] = ndb.DateTimeProperty(auto_now= True)
attrs['_get_kind'] = classmethod(get_kind)
attrs['__name__'] = name
attr_meta = attrs.get('Meta',None)
if attr_meta is None:
meta = type('meta',(object,),dict())
else:
meta = attr_meta
kwargs= {}
model_module = sys.modules[attrs['__module__']]
kwargs['app_label'] = model_module.__name__.split('.')[-2]
_meta = Options(meta,name,**kwargs)
attrs['_meta'] = _meta
return type(name,(ndb.Model,),attrs)
class Model(object):
__metaclass__ = MetaModel
class TesTModel(Model):
name = ndb.StringProperty(indexed=False)
tm = TestModel(name='This is the test model')
tm.put()
This seems pretty fragile. Sounds like an expando model might work for you?
Edit (based on clarification below): metaclasses and type really should be a last resort. They're confusing and hard to get right. For example, in your code snippet, subclasses of Model get ndb's metaclass instead of the one above:
class T1(Model):
pass
>>> T1().to_dict()
T1 {'date_upd': None, 'date_add': None}
class T2(Model):
auto_date_time = False
class T3(T2):
auto_date_time = True
>>> T2.__metaclass__
<class 'ndb.model.MetaModel'>
>>> T3().to_dict()
{}
You can avoid some craziness by deriving your metaclass from ndb.MetaModel:
class MyMetaModel(ndb.MetaModel):
def __new__(cls, name, bases, attrs):
return super(MyMetaModel,cls).__new__(cls, name, bases, attrs)
class MyModel(ndb.Model):
__metaclass__ = MyMetaModel
class AutoTimeModel(MyModel):
date_add = ndb.DateTimeProperty(auto_now_add=True)
date_upd = ndb.DateTimeProperty(auto_now=True)
I have two models:
class ModelOne(models.Model):
something = models.TextField[...]
class ModelTwo(models.Model):
other_something = models.TextField[...]
ref = models.ForeignKey(ModelOne)
And I want to write function in ModelOne which return me all related objects from ModelTwo.
It's important: In ModelOne.
How to do it?
Invoke self.modeltwo_set.all().