Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Models with a CharField as primary key #56

Closed
ostcar opened this issue Oct 21, 2013 · 2 comments
Closed

Models with a CharField as primary key #56

ostcar opened this issue Oct 21, 2013 · 2 comments

Comments

@ostcar
Copy link

ostcar commented Oct 21, 2013

If the primary key of a model with a JSONField is a CharField, a ValidateError occurs when setting the jsonfield.

Here are some tests that fail:

class JsonModelWithPK(models.Model):
    json = JSONField()
    key = models.CharField(max_length=255, primary_key=True)

class JSONPKTest(JSONFieldTest):
    json_model = JsonModelWithPK
@ostcar
Copy link
Author

ostcar commented Oct 27, 2013

I played arround some more. The error has nothing to do with the CharField. It occurs when setting the primary key of the model. Here is a test that fails (in his first line):

    def test_set_pk(self):
        self.json_model.objects.create(id=42, json='test string')
        new_obj = self.json_model.objects.get(id=42)
        self.assertEqual(new_obj.json, 'test string')

The problem is related to #33. The fix from that issue does not help in this case, because In this line, the obj has a pk: https://github.com/bradjasper/django-jsonfield/blob/master/jsonfield/fields.py#L72

A workaround is to use ...create(pk=42, json='test_string').

@bradjasper
Copy link
Collaborator

@ostcar unfortunately I'm not sure how to solve this one.

The problem is being able to determine when a form is new vs when it's already been saved. This is needed to know when a string has already been JSON encoded.

In this case, you're passing an ID in which is tripping up the mechanism we use to perform this check.

Since there is a workaround, I'm going to suggest using that and close this. Sorry I don't have a better answer here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants