<p>Given a Django.db models class:</p>

<pre><code>class P(models.Model):
   type = models.ForeignKey(Type) # Type is another models.Model class
   name = models.CharField()
</code></pre>

<p>where one wishes to create a new P with a specified type, i.e. how does one make "type" to be a default, hidden field (from the user), where type is given likeso:</p>

<pre><code>http://x.y/P/new?type=3 
</code></pre>

<p>So that in the form no "type" field will appear, but when the P is saved, its type will have id 3 (i.e. Type.objects.get(pk=3)).</p>

<p>Secondarily, how does one (&amp; is it possible) specify a "default" type in the url, via urls.py, when using generic Django views, viz.</p>

<pre><code>urlpatterns = ('django.generic.views.create_update',
  url(r'^/new$', 'create_object', { 'model': P }, name='new_P'),
)
</code></pre>

<p>I found that terribly difficult to describe, which may be part of the problem. :) Input is much appreciated!</p>

To have a default Foreign Key in a model:
```
```
mydefault = Type.objects.get(pk=3)

class P(models.Model):
   type = models.ForeignKey(Type, default=mydefault) # Type is another models.Model class
   name = models.CharField()
```
```
Note that using
```
pk=x
```
is pretty ugly, as ideally you shouldn't care what the primary key is equal to.  Try to get to the object you want by some other attribute.
Here
's how you put defaults in your urls:
```
```
# URLconf
urlpatterns = patterns('',
    (r'^blog/$', 'blog.views.page'),
    (r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
)

# View (in blog/views.py)
def page(request, num="1"):
    # Output the appropriate page of blog entries, according to num.
```
```
In the above example, both URL patterns point to the same view -- blog.views.page -- but the first pattern doesn't capture anything from the URL. If the first pattern matches, the page() function will use its default argument for num, "1". If the second pattern matches, page() will use whatever num value was captured by the regex.