question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not have a field named the same as a Page model

See original GitHub issue

The following model definition will cause an error when instansiating a Foo:

from django.db import models
from wagtail.wagtailcore.models import Page


class Foo(Page):
    bar = models.CharField(max_length=255)


class Bar(Page):
    pass
$ ./manage.py shell
Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from wagtailtests.website.models import Foo, Bar
>>> Bar()
<Bar: >
>>> Foo()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/vagrant/wagtail/wagtailcore/models.py", line 305, in __init__
    super(Page, self).__init__(*args, **kwargs)
  File "/home/vagrant/wagtailtests_venv/local/lib/python2.7/site-packages/modelcluster/models.py", line 114, in __init__
    super(ClusterableModel, self).__init__(*args, **kwargs)
  File "/home/vagrant/wagtailtests_venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 407, in __init__
    setattr(self, field.attname, val)
  File "/home/vagrant/wagtailtests_venv/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 223, in __set__
    self.related.get_accessor_name(), self.related.opts.object_name))
ValueError: Cannot assign "u''": "Foo.bar" must be a "Bar" instance.

This does not happen when using django.db.models.Model as the base class of Foo and Bar, so this is a Wagtail issue. I encountered this issue when attempting to create a Page model named Website, and had another Page model with a field website.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:2
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
kaedrohocommented, Feb 18, 2015

I’ve found another issue recently that is related to this.

It is not possible to have two page classes with the same name, even if they’re in different apps. This is because the reverse accessor from the Page class would be given the same name causing a clash.

This is possible to work around using the code above to override page_ptr. It is not possible to work around it if your page type decends from an abstract class (form pages for example). The only thing you can do is rename your models.

1reaction
mx-mothcommented, Jul 29, 2014

I managed to work around this issue by overriding the automatically generated OneToOneField for Multi-table inheritance, and disabling the reverse relation:

from django.db import models
from wagtail.wagtailcore.models import Page

class Foo(Page):
    page_ptr = models.OneToOneField(Page, parent_link=True, related_name='+')
    bar = models.CharField(max_length=255)

class Bar(Page):
    page_ptr = models.OneToOneField(Page, parent_link=True, related_name='+')

page_ptr is the name Django automatically generates for this field. Keeping it the same in the overridden field should stop anything strange from happening. I am trying to see if there is any simple way of placing this on all models automatically through subclassing or some such to disable this application wide.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fields Are Not Visible on a New Page Layout for Certain Users
Click the name of the user; Click Profile. Under Apps, select Object Settings then click the object; Click Edit; Select Read box for...
Read more >
Razor View throwing "The name 'model' does not exist in the ...
Create a new project targeting the same .NET framework and copy its Views/web.config file on top of the one in your current project....
Read more >
Introduction to data types and field properties - Microsoft Support
Every table in Access is made up of fields. The properties of a field describe the characteristics and behavior of data added to...
Read more >
Handler Methods in Razor Pages
You cannot have both in the same page. If you do, the framework will ... Razor Pages includes a feature called "named handler...
Read more >
How to Use Fields | Form Designer | User Guide | Epi Info - CDC
Field names cannot start with a number or contain any spaces or non-alphanumeric characters (except the underscore character “_” is permitted). The field...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found