Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a foreign key into a form

I'm new to Django and Python, and have spent a couple days trying to solve this problem. I've tried several approaches in other threads here, but I think I'm missing some basic concept because none of them have worked for me. Here's what I'm trying to do:

I'm using ModelForm, and I have created a form for adding records that includes a foreign key. I want this value to be set by default to match the record that was displayed on the page I linked from. [Code shown below edited to show suggested corrections].

Here's the relevant code from views.py:

def add_sighting(request):
unit = request.GET.get('unit')
form = SightingForm(request.POST or None, initial={'unit':unit})
if form.is_valid():
    sighting = form.save(commit=False)
    sighting.save()
    return redirect(index)
return render_to_response('trainapp/add_sighting_form.html',
                          {'unit': unit, 'sighting_form': form},
                          context_instance=RequestContext(request))

Here's the relevant code from add_sighting_form.html:

<form action="/trainapp/add_sighting/" method="post">
{% csrf_token %}
<table>
{{ sighting_form.as_table }}
</table>
<input type="submit" value="Save" />
</form>

Here's the relevant code from the template I linked from:

<p><a href="/trainapp/add_sighting/?unit={{ unit.id }}">Add sighting</a></p>
like image 703
Jonathan Rickard Avatar asked Oct 10 '12 21:10

Jonathan Rickard


People also ask

What can you do with a foreign key?

Foreign keys link data in one table to the data in another table. A foreign key column in a table points to a column with unique values in another table (often the primary key column) to create a way of cross-referencing the two tables.

How do you create a foreign key in access?

Select one or more tables or queries and then click Add. After you have finished adding tables and queries to the Relationships document tab, click Close. Drag a field (typically the primary key) from one table to the common field (the foreign key) in the other table.

When to add a foreign key?

Foreign key columns are frequently used in join criteria when the data from related tables is combined in queries by matching the column or columns in the foreign key constraint of one table with the primary or unique key column or columns in the other table.

What is a foreign key and why might we want to use one?

A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It acts as a cross-reference between tables because it references the primary key of another table, thereby establishing a link between them.


1 Answers

Several approaches might work here.

The easiest one is using the initial parameter when creating an instance of the form before you pass it into the context of some view.

Say you are using a view method as follow:

def some_view( request ):
    id = request.GET.get( 'record-id' )
    # do check if id is valid...

    # load some record
    record = Record.objects.get( pk=id )

    # create a form instance
    form = RecordForm( initial={ 'some_key_field_name':record.id } )

    # set up context
    context = Context({
         'record' : record,
         'form' : form
    })

    # render the templateview using the above context
    # and return a HttpResponse ...

If you are using class based views instead, you might override get_context_data to get a similar result.

Class based views

Based on your code I also want to suggest you take a look at class based views in Django. The can handle getting and posting forms for you. It even takes care of creating the (default) form for you.

The equivalent of the posted code would be something like this:

class CreateSightingView( CreateView ):

    model = Sighting

    def get_context_data( self, **kwargs ):
        # call the super to get the default context
        data = super( CreateSightingView, self ).get_context_data( **kwargs )

        # add the initial value here
        # but first get the id from the request GET data
        id = self.request.GET.get( 'record-id' )
        # again: do some checking of id
        data.get( 'form' ).initial[ 'unit' ] = id

        return data
like image 144
A.J.Rouvoet Avatar answered Sep 28 '22 08:09

A.J.Rouvoet