0

i have a db in PostgreSQL which is connecting 2 different tables lets call them TBL1 ( TBL1 is a table for all types ) and TBL2( TBL2 is a table for e.g. cars ), TBL1 and TBL2 have a same column named type, so i have created the foreign key between them. Because i'm not sure if that has anything to do with Django...

So, now in model, i have created ( based on the form ) variable called type = models.ForeignKey('TBL1', db_column='type', related_name='type') and it's returning to me Django object model ForeignKey, which i guess is ok. I would like to show that in a form like select and i don't know how to get the data from TBL1. Is there any way to do this, because i have searched for 2-3 days and trying other options, but non of them did the trick ? I have find out that e.g.

STATUS_CHOICES = (
        ('new', _('New')),
        ('purchased', _('Purchased'))
      )

status = models.CharField(max_length=200, default='new', choices=STATUS_CHOICES)

Is showing the select in form :

Select with HC data

P.S.

Django version is 1.5.4 and Python 2.7 , i have never worked in Django or PostgreSQL, so any help would be appreciated. Regards

crazy_ljuba
  • 104
  • 8
  • Your form should already create a ChoiceField with value populated from DB for you. Make sure you're using `ModelForm`, and it'll be taken care of. – Rohit Jain Mar 22 '16 at 17:49
  • Are `TBL1` and `TBL2` Tables that were created by django, or were they created before hand? – Tim Mar 22 '16 at 22:46
  • @Rohit will look to see, it's not my code, i'm just fixing the bugs xD – crazy_ljuba Mar 23 '16 at 07:26
  • @Tim I created the tables, and don't know if there is something else to do... – crazy_ljuba Mar 23 '16 at 07:28
  • Ok, maybe my question wasn't completely clear: In your `models.py`, did you create the classes `class TBL1(models.Model)` und `class TBL2(models.Model)` or do they just exist in the database? – Tim Mar 23 '16 at 07:43
  • That's another problem :) I have one model.py where i have data extracted from **TBL1** and another model.py where i extracted **TBL2** ( those models are in different folders so i named them **model1.py** and **model2.py** ). Now because **model1.py** imports some class from **model2.py** i can't get data from **model2.py** inside **model1.py**, it's giving me some error about cannot load model... I hope you can understand what i mean in this whole mambo jumbo thing i wrote :) – crazy_ljuba Mar 23 '16 at 10:15
  • @RohitJain there is no model.ChoiceField ( if i understood you right ), but that's not the issue, the main issue is that i don't know how to extract the data from the foreignKey and go into the for loop, to get the data from DB `type = models.ForeignKey('TBL1', db_column='type', related_name='type')` I have tried to show in the console some data, but e.g. name of it is **null** ( **type.name** ) – crazy_ljuba Mar 23 '16 at 11:11

1 Answers1

0

So after doing some more research , i have stumbled upon link , where it's nicely explained how you can edit the choices "inline" inside admin.py , although i was waisting time trying to add the data from model.py

class FooForm(forms.ModelForm):
class Meta:
    model = Foo

def __init__(self, *args, **kwargs):
    super(FooForm, self).__init__(*args, **kwargs)
    current_state = self.instance.state
    ...construct available_choices based on current state...
    self.fields['state'].choices = available_choices

And it worked just as i hoped to work :)

So thanks a lot guys for trying to help, i'm not python/django dev, so this all are not similar syntax to me at all :)

Best all

Community
  • 1
  • 1
crazy_ljuba
  • 104
  • 8