Invalid ObjectId when saving to a ReferenceField in Mongo

874 views Asked by At

so I have this model set up with django and mongoengine.

class Product(Document):
    product_id = IntField()

    title = StringField(max_length=255)

    sources = ListField(ReferenceField(Source, dbref = True))

class Source(Document):
    source_id = IntField()
    source_type = StringField(choices=settings.PARENT_TYPE_CHOICES, max_length=50)
    name = StringField(max_length=255)
    url = URLField(max_length=2000)

    meta = {"allow_inheritance": True}

And in my scrapy pipeline I save the following data:

class SaveItemPipeline(object):
    def process_item(self, item, spider):
        product = item["product"]
        product["sources"] = self.create_sources(product)
        saved_product,created = Product.objects.get_or_create(**product)

        return item

    def create_sources(self,product):
        temp_sources = []
        for source in product["sources"]:
            print source
            if source["source_type"] == "user":
                temp_source,created = UserSource.objects.get_or_create(**source)
            elif source["source_type"] == "store":
                temp_source,created = StoreSource.objects.get_or_create(**source)
            elif source["source_type"] == "collection":
                temp_source,created = CollectionSource.objects.get_or_create(**source)
            temp_sources.append(temp_source.id)
        return temp_sources

Howerver, when I run the scraper, on save it gives me this error:

raise ValidationError(message, errors=errors, field_name=field_name) mongoengine.errors.ValidationError: [ObjectId('55787a07516ddcf4d93cd4c6'), ObjectId('55787b07516ddcf5aff06fa9'), ObjectId('55787b07516ddcf5aff06faa')] is not a valid ObjectId

By the way the UserSource and StoreSource...all inherit from Source so they are just subclasses.However, am I doing anything wrong here, I don't understand why it is giving me that error when product gets created.

Thanks!

1

There are 1 answers

0
Rajesh Kaushik On

You can use this

class Source(Document):
    source_id = IntField()
class Product(Document):
    sources = ListField(ReferenceField(Source, dbref = True))
src, created = Source.objects.create(source_id=1)
pd, _ = Product.objects.create(sources=[src])

It works for me. I am using mongoengine 0.8.7, pymongo 2.8