How to solve Unknow Field Error in Django












1















I am building a project a project by Django 2.0.2. What I want to do is making one of the fieldsets in admin page change automatically according to the database. Here is part of my code:



models.py
class Product(BaseModel):
number=model.IntergerField("Food", null=True, blank=True, help_text="Food")

@property
def number(self):
return 10 #just return a number for testing

admin.py
class ProductAdmin(admin.ModelAdmin):
list_display=("number")
fieldsets=(('<h1>Food</h1>'), {'fields':('number')})


When I refresh the admin page, it shows "Unkown fields (number) specified for Product. Check fieldset attribute of class ProductAdmin". I am new to Django and not sure about what goes wrong. In the admin.py, I have added "number" field but the error still exist. How can I solve it? Many Thanks and Happy New Year.










share|improve this question



























    1















    I am building a project a project by Django 2.0.2. What I want to do is making one of the fieldsets in admin page change automatically according to the database. Here is part of my code:



    models.py
    class Product(BaseModel):
    number=model.IntergerField("Food", null=True, blank=True, help_text="Food")

    @property
    def number(self):
    return 10 #just return a number for testing

    admin.py
    class ProductAdmin(admin.ModelAdmin):
    list_display=("number")
    fieldsets=(('<h1>Food</h1>'), {'fields':('number')})


    When I refresh the admin page, it shows "Unkown fields (number) specified for Product. Check fieldset attribute of class ProductAdmin". I am new to Django and not sure about what goes wrong. In the admin.py, I have added "number" field but the error still exist. How can I solve it? Many Thanks and Happy New Year.










    share|improve this question

























      1












      1








      1








      I am building a project a project by Django 2.0.2. What I want to do is making one of the fieldsets in admin page change automatically according to the database. Here is part of my code:



      models.py
      class Product(BaseModel):
      number=model.IntergerField("Food", null=True, blank=True, help_text="Food")

      @property
      def number(self):
      return 10 #just return a number for testing

      admin.py
      class ProductAdmin(admin.ModelAdmin):
      list_display=("number")
      fieldsets=(('<h1>Food</h1>'), {'fields':('number')})


      When I refresh the admin page, it shows "Unkown fields (number) specified for Product. Check fieldset attribute of class ProductAdmin". I am new to Django and not sure about what goes wrong. In the admin.py, I have added "number" field but the error still exist. How can I solve it? Many Thanks and Happy New Year.










      share|improve this question














      I am building a project a project by Django 2.0.2. What I want to do is making one of the fieldsets in admin page change automatically according to the database. Here is part of my code:



      models.py
      class Product(BaseModel):
      number=model.IntergerField("Food", null=True, blank=True, help_text="Food")

      @property
      def number(self):
      return 10 #just return a number for testing

      admin.py
      class ProductAdmin(admin.ModelAdmin):
      list_display=("number")
      fieldsets=(('<h1>Food</h1>'), {'fields':('number')})


      When I refresh the admin page, it shows "Unkown fields (number) specified for Product. Check fieldset attribute of class ProductAdmin". I am new to Django and not sure about what goes wrong. In the admin.py, I have added "number" field but the error still exist. How can I solve it? Many Thanks and Happy New Year.







      python django database django-models django-admin






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 12:06









      WILLIAMWILLIAM

      496




      496
























          1 Answer
          1






          active

          oldest

          votes


















          1














          number is not a field, it is a property. You can add this to the readonly_fields:



          class ProductAdmin(admin.ModelAdmin):
          list_display=('number',)
          readonly_fields = ('number',)
          fieldsets=(('<h1>Food</h1>'), {'fields':('number')})


          You should also add a short_description to your property, like:



          class Product(BaseModel):
          number=model.IntergerField("Food", null=True, blank=True, help_text="Food")

          # no decorator
          def number(self):
          return 10 #just return a number for testing
          number.short_description = "The number"
          number = property(number)


          as is documented in the ModelAdmin options section.






          share|improve this answer


























          • Thanks. It work when I added readonly_fields as you did.

            – WILLIAM
            Jan 2 at 5:49











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995295%2fhow-to-solve-unknow-field-error-in-django%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          number is not a field, it is a property. You can add this to the readonly_fields:



          class ProductAdmin(admin.ModelAdmin):
          list_display=('number',)
          readonly_fields = ('number',)
          fieldsets=(('<h1>Food</h1>'), {'fields':('number')})


          You should also add a short_description to your property, like:



          class Product(BaseModel):
          number=model.IntergerField("Food", null=True, blank=True, help_text="Food")

          # no decorator
          def number(self):
          return 10 #just return a number for testing
          number.short_description = "The number"
          number = property(number)


          as is documented in the ModelAdmin options section.






          share|improve this answer


























          • Thanks. It work when I added readonly_fields as you did.

            – WILLIAM
            Jan 2 at 5:49
















          1














          number is not a field, it is a property. You can add this to the readonly_fields:



          class ProductAdmin(admin.ModelAdmin):
          list_display=('number',)
          readonly_fields = ('number',)
          fieldsets=(('<h1>Food</h1>'), {'fields':('number')})


          You should also add a short_description to your property, like:



          class Product(BaseModel):
          number=model.IntergerField("Food", null=True, blank=True, help_text="Food")

          # no decorator
          def number(self):
          return 10 #just return a number for testing
          number.short_description = "The number"
          number = property(number)


          as is documented in the ModelAdmin options section.






          share|improve this answer


























          • Thanks. It work when I added readonly_fields as you did.

            – WILLIAM
            Jan 2 at 5:49














          1












          1








          1







          number is not a field, it is a property. You can add this to the readonly_fields:



          class ProductAdmin(admin.ModelAdmin):
          list_display=('number',)
          readonly_fields = ('number',)
          fieldsets=(('<h1>Food</h1>'), {'fields':('number')})


          You should also add a short_description to your property, like:



          class Product(BaseModel):
          number=model.IntergerField("Food", null=True, blank=True, help_text="Food")

          # no decorator
          def number(self):
          return 10 #just return a number for testing
          number.short_description = "The number"
          number = property(number)


          as is documented in the ModelAdmin options section.






          share|improve this answer















          number is not a field, it is a property. You can add this to the readonly_fields:



          class ProductAdmin(admin.ModelAdmin):
          list_display=('number',)
          readonly_fields = ('number',)
          fieldsets=(('<h1>Food</h1>'), {'fields':('number')})


          You should also add a short_description to your property, like:



          class Product(BaseModel):
          number=model.IntergerField("Food", null=True, blank=True, help_text="Food")

          # no decorator
          def number(self):
          return 10 #just return a number for testing
          number.short_description = "The number"
          number = property(number)


          as is documented in the ModelAdmin options section.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 1 at 12:21

























          answered Jan 1 at 12:11









          Willem Van OnsemWillem Van Onsem

          149k16144233




          149k16144233













          • Thanks. It work when I added readonly_fields as you did.

            – WILLIAM
            Jan 2 at 5:49



















          • Thanks. It work when I added readonly_fields as you did.

            – WILLIAM
            Jan 2 at 5:49

















          Thanks. It work when I added readonly_fields as you did.

          – WILLIAM
          Jan 2 at 5:49





          Thanks. It work when I added readonly_fields as you did.

          – WILLIAM
          Jan 2 at 5:49




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995295%2fhow-to-solve-unknow-field-error-in-django%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas