hmarr / mongoengine

[Moved to mongoengine/mongoengine]
https://github.com/MongoEngine/mongoengine
MIT License
793 stars 20 forks source link

ValidationError(A ReferenceField only accepts DBRef or documents: ['user']) problem #576

Closed burakkilic closed 11 years ago

burakkilic commented 12 years ago
class Activity(Document):
    meta = {'allow_inheritance': True}
    user = ReferenceField(User, dbref=True)

When I am trying to insert a new Activity, I am getting ValidationError. What is the problem?

ghost commented 12 years ago

Since you are using ReferenceField, you should assign the user field a user.to_dbref() value. Otherwise, you should make it required = False, if activities do not require an assigned user reference.

burakkilic commented 11 years ago

I tried to_dbref(). I doesn't work.

rating = Activity.objects.create(venue=venue.to_dbref(),user=user.to_dbref())

My User registration is like:

def register(request):
    if request.method == 'POST' and request.POST['name'] and request.POST['password']:
        u = User(username=request.POST['name'])
        u.set_password(request.POST['password'])
        u.save()
    return render_to_response('registration/register.html')

And User model is:

class User(auth.User):
    email = EmailField()
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)
    photo = URLField()

auth.User is mongoengine' s User.

rozza commented 11 years ago

You should just pass a saved user object and it will convert correctly. If this is still an issue - please post a ticket to the official repo: https://github.com/MongoEngine/mongoengine