Django model form field to have a user dropdown list based on a conditiondjango - inlineformset_factory with more than one ForeignKeySaving form data rewrites the same rowRadio buttons in django adminCreate a new model which have all fields of currently existing model'NoneType' object is not subscriptable in using django smart selectsHow to expose some specific fields of model_b based on a field of model_a?How to set dynamic initial values to django modelform fieldHow to define Mode with generic ForeignKey in DjangoHow to create an incremental id for a set of projects per user in Django?f string interpolation syntax error python3.6

What is this word supposed to be?

Where was the County of Thurn und Taxis located?

What is the unit of time_lock_delta in LND?

Contradiction proof for inequality of P and NP?

How exactly does Hawking radiation decrease the mass of black holes?

What is the best way to deal with NPC-NPC combat?

Mistake in years of experience in resume?

Why is the underscore command _ useful?

Magical attacks and overcoming damage resistance

What is the most expensive material in the world that could be used to create Pun-Pun's lute?

How do I check if a string is entirely made of the same substring?

Retract an already submitted recommendation letter (written for an undergrad student)

std::unique_ptr of base class holding reference of derived class does not show warning in gcc compiler while naked pointer shows it. Why?

How to have a sharp product image?

Can a stored procedure reference the database in which it is stored?

What makes accurate emulation of old systems a difficult task?

Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?

Which big number is bigger?

Multiple fireplaces in an apartment building?

Would the change in enthalpy (ΔH) for the dissolution of urea in water be positive or negative?

How can I wire a 9-position switch so that each position turns on one more LED than the one before?

Nails holding drywall

Find a stone which is not the lightest one

What does "function" actually mean in music?



Django model form field to have a user dropdown list based on a condition


django - inlineformset_factory with more than one ForeignKeySaving form data rewrites the same rowRadio buttons in django adminCreate a new model which have all fields of currently existing model'NoneType' object is not subscriptable in using django smart selectsHow to expose some specific fields of model_b based on a field of model_a?How to set dynamic initial values to django modelform fieldHow to define Mode with generic ForeignKey in DjangoHow to create an incremental id for a set of projects per user in Django?f string interpolation syntax error python3.6






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















In a Django Modelform (Product_definition), i want to have a dropdown(Merchant name) which will show users only if the their designation in User form is "Merchant".
is it possible that I could get the list of users for the dropdown based on this condition .Please note that i don't require it to be a foreign key as connecting the models is not required.
This is the form which contains the Designation :



from django.contrib.auth.models import User

class UserProfileInfo(models.Model):
user = models.OneToOneField(User,on_delete = models.CASCADE)

#extra UserAttribute
MERCHANT = 'MR'
FABRIC = 'FR'
WASHING = 'WS'
PRINT = 'PR'
PLANNER = 'PL'

DESIGNATION_CHOICES =(
(PLANNER,'Planner'),
(MERCHANT,'Merchant'),
(FABRIC,'Fabric'),
(WASHING,'Washing'),
(PRINT,'Printing'),

)

Designation =models.CharField(
max_length = 20,
choices = DESIGNATION_CHOICES,
default= 'PLANNER'
)
def __str__(self):
return self.user.username


and this is the form with Merchant Name where I want the names of all merchants to appear.



class Product_definition(models.Model):

Order_number = models.CharField(max_length=25,unique = True, blank = True, null = True)
style_name = models.CharField(max_length=15, blank = True, null = True)
color = models.CharField(max_length=15, blank = True, null = True)
Order_qty = models.PositiveIntegerField()
SMV = models.FloatField()
MERCHANT = models.ForeignKey(UserProfileInfo,on_delete= models.CASCADE,default='Select')
def __str__(self):
return self.Order_number


I have created a foreign key for now but I don't require it and it doesn't list the names of only the merchant in the drop down.










share|improve this question






























    0















    In a Django Modelform (Product_definition), i want to have a dropdown(Merchant name) which will show users only if the their designation in User form is "Merchant".
    is it possible that I could get the list of users for the dropdown based on this condition .Please note that i don't require it to be a foreign key as connecting the models is not required.
    This is the form which contains the Designation :



    from django.contrib.auth.models import User

    class UserProfileInfo(models.Model):
    user = models.OneToOneField(User,on_delete = models.CASCADE)

    #extra UserAttribute
    MERCHANT = 'MR'
    FABRIC = 'FR'
    WASHING = 'WS'
    PRINT = 'PR'
    PLANNER = 'PL'

    DESIGNATION_CHOICES =(
    (PLANNER,'Planner'),
    (MERCHANT,'Merchant'),
    (FABRIC,'Fabric'),
    (WASHING,'Washing'),
    (PRINT,'Printing'),

    )

    Designation =models.CharField(
    max_length = 20,
    choices = DESIGNATION_CHOICES,
    default= 'PLANNER'
    )
    def __str__(self):
    return self.user.username


    and this is the form with Merchant Name where I want the names of all merchants to appear.



    class Product_definition(models.Model):

    Order_number = models.CharField(max_length=25,unique = True, blank = True, null = True)
    style_name = models.CharField(max_length=15, blank = True, null = True)
    color = models.CharField(max_length=15, blank = True, null = True)
    Order_qty = models.PositiveIntegerField()
    SMV = models.FloatField()
    MERCHANT = models.ForeignKey(UserProfileInfo,on_delete= models.CASCADE,default='Select')
    def __str__(self):
    return self.Order_number


    I have created a foreign key for now but I don't require it and it doesn't list the names of only the merchant in the drop down.










    share|improve this question


























      0












      0








      0








      In a Django Modelform (Product_definition), i want to have a dropdown(Merchant name) which will show users only if the their designation in User form is "Merchant".
      is it possible that I could get the list of users for the dropdown based on this condition .Please note that i don't require it to be a foreign key as connecting the models is not required.
      This is the form which contains the Designation :



      from django.contrib.auth.models import User

      class UserProfileInfo(models.Model):
      user = models.OneToOneField(User,on_delete = models.CASCADE)

      #extra UserAttribute
      MERCHANT = 'MR'
      FABRIC = 'FR'
      WASHING = 'WS'
      PRINT = 'PR'
      PLANNER = 'PL'

      DESIGNATION_CHOICES =(
      (PLANNER,'Planner'),
      (MERCHANT,'Merchant'),
      (FABRIC,'Fabric'),
      (WASHING,'Washing'),
      (PRINT,'Printing'),

      )

      Designation =models.CharField(
      max_length = 20,
      choices = DESIGNATION_CHOICES,
      default= 'PLANNER'
      )
      def __str__(self):
      return self.user.username


      and this is the form with Merchant Name where I want the names of all merchants to appear.



      class Product_definition(models.Model):

      Order_number = models.CharField(max_length=25,unique = True, blank = True, null = True)
      style_name = models.CharField(max_length=15, blank = True, null = True)
      color = models.CharField(max_length=15, blank = True, null = True)
      Order_qty = models.PositiveIntegerField()
      SMV = models.FloatField()
      MERCHANT = models.ForeignKey(UserProfileInfo,on_delete= models.CASCADE,default='Select')
      def __str__(self):
      return self.Order_number


      I have created a foreign key for now but I don't require it and it doesn't list the names of only the merchant in the drop down.










      share|improve this question
















      In a Django Modelform (Product_definition), i want to have a dropdown(Merchant name) which will show users only if the their designation in User form is "Merchant".
      is it possible that I could get the list of users for the dropdown based on this condition .Please note that i don't require it to be a foreign key as connecting the models is not required.
      This is the form which contains the Designation :



      from django.contrib.auth.models import User

      class UserProfileInfo(models.Model):
      user = models.OneToOneField(User,on_delete = models.CASCADE)

      #extra UserAttribute
      MERCHANT = 'MR'
      FABRIC = 'FR'
      WASHING = 'WS'
      PRINT = 'PR'
      PLANNER = 'PL'

      DESIGNATION_CHOICES =(
      (PLANNER,'Planner'),
      (MERCHANT,'Merchant'),
      (FABRIC,'Fabric'),
      (WASHING,'Washing'),
      (PRINT,'Printing'),

      )

      Designation =models.CharField(
      max_length = 20,
      choices = DESIGNATION_CHOICES,
      default= 'PLANNER'
      )
      def __str__(self):
      return self.user.username


      and this is the form with Merchant Name where I want the names of all merchants to appear.



      class Product_definition(models.Model):

      Order_number = models.CharField(max_length=25,unique = True, blank = True, null = True)
      style_name = models.CharField(max_length=15, blank = True, null = True)
      color = models.CharField(max_length=15, blank = True, null = True)
      Order_qty = models.PositiveIntegerField()
      SMV = models.FloatField()
      MERCHANT = models.ForeignKey(UserProfileInfo,on_delete= models.CASCADE,default='Select')
      def __str__(self):
      return self.Order_number


      I have created a foreign key for now but I don't require it and it doesn't list the names of only the merchant in the drop down.







      python django django-models django-forms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 10 at 3:20









      Konchitha Pradeep

      173




      173










      asked Mar 9 at 7:13









      Jhonty4Jhonty4

      13




      13






















          1 Answer
          1






          active

          oldest

          votes


















          1














          I think you can do it like this using ModelChoiceField:



          class ProductForm(forms.ModelForm): # please use CamelCase when defining Class Names
          MERCHANT = forms.ModelChoiceField(queryset=UserProfileInfo.objects.filter(Designation=UserProfileInfo.MARCHENT)) # Please use sname_case when naming attributes
          class Meta:
          model = Product_definition # Please use CamelCase when defining model class name
          fields = '__all__'





          share|improve this answer























          • When trying to migrate i am getting "invalid literal for int () with base 10" . Before adding this form I was able to migrate. I have also replaced all the integer field to FloatField but it didn't resolve the error

            – Jhonty4
            Mar 25 at 8:41











          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%2f55074958%2fdjango-model-form-field-to-have-a-user-dropdown-list-based-on-a-condition%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














          I think you can do it like this using ModelChoiceField:



          class ProductForm(forms.ModelForm): # please use CamelCase when defining Class Names
          MERCHANT = forms.ModelChoiceField(queryset=UserProfileInfo.objects.filter(Designation=UserProfileInfo.MARCHENT)) # Please use sname_case when naming attributes
          class Meta:
          model = Product_definition # Please use CamelCase when defining model class name
          fields = '__all__'





          share|improve this answer























          • When trying to migrate i am getting "invalid literal for int () with base 10" . Before adding this form I was able to migrate. I have also replaced all the integer field to FloatField but it didn't resolve the error

            – Jhonty4
            Mar 25 at 8:41















          1














          I think you can do it like this using ModelChoiceField:



          class ProductForm(forms.ModelForm): # please use CamelCase when defining Class Names
          MERCHANT = forms.ModelChoiceField(queryset=UserProfileInfo.objects.filter(Designation=UserProfileInfo.MARCHENT)) # Please use sname_case when naming attributes
          class Meta:
          model = Product_definition # Please use CamelCase when defining model class name
          fields = '__all__'





          share|improve this answer























          • When trying to migrate i am getting "invalid literal for int () with base 10" . Before adding this form I was able to migrate. I have also replaced all the integer field to FloatField but it didn't resolve the error

            – Jhonty4
            Mar 25 at 8:41













          1












          1








          1







          I think you can do it like this using ModelChoiceField:



          class ProductForm(forms.ModelForm): # please use CamelCase when defining Class Names
          MERCHANT = forms.ModelChoiceField(queryset=UserProfileInfo.objects.filter(Designation=UserProfileInfo.MARCHENT)) # Please use sname_case when naming attributes
          class Meta:
          model = Product_definition # Please use CamelCase when defining model class name
          fields = '__all__'





          share|improve this answer













          I think you can do it like this using ModelChoiceField:



          class ProductForm(forms.ModelForm): # please use CamelCase when defining Class Names
          MERCHANT = forms.ModelChoiceField(queryset=UserProfileInfo.objects.filter(Designation=UserProfileInfo.MARCHENT)) # Please use sname_case when naming attributes
          class Meta:
          model = Product_definition # Please use CamelCase when defining model class name
          fields = '__all__'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 9 at 8:37









          ruddraruddra

          17.3k42951




          17.3k42951












          • When trying to migrate i am getting "invalid literal for int () with base 10" . Before adding this form I was able to migrate. I have also replaced all the integer field to FloatField but it didn't resolve the error

            – Jhonty4
            Mar 25 at 8:41

















          • When trying to migrate i am getting "invalid literal for int () with base 10" . Before adding this form I was able to migrate. I have also replaced all the integer field to FloatField but it didn't resolve the error

            – Jhonty4
            Mar 25 at 8:41
















          When trying to migrate i am getting "invalid literal for int () with base 10" . Before adding this form I was able to migrate. I have also replaced all the integer field to FloatField but it didn't resolve the error

          – Jhonty4
          Mar 25 at 8:41





          When trying to migrate i am getting "invalid literal for int () with base 10" . Before adding this form I was able to migrate. I have also replaced all the integer field to FloatField but it didn't resolve the error

          – Jhonty4
          Mar 25 at 8:41



















          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%2f55074958%2fdjango-model-form-field-to-have-a-user-dropdown-list-based-on-a-condition%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

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved