Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 June 21, 2010 15:38:47

Magnus V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

optional ForeignKey


Hi,
I'm trying to make model that has itself as a foreign key, in an
attempt to store a reverse tree in Django.
However, I can't find a way to set a default value to
models.ForeignKey or make it optional.

My model is like this:
class Node(models.Model):
name = models.CharField(max_length=100)
parent = models.ForeignKey('self')

runserver doesn't complain, but whenever I try to enter data to the
model with Django's admin interface I get a "This field is required."
message, and it want's me to select a entry, but there are none.

--
Magnus

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 21, 2010 15:40:58

e.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

optional ForeignKey


You want:

parent = models.ForeignKey('self', null=True, blank=True)

On Jun 21, 3:37 pm, Magnus Valle <wisca...@gmail.com> wrote:
> Hi,
> I'm trying to make model that has itself as a foreign key, in an
> attempt to store a reverse tree in Django.
> However, I can't find a way to set a default value to
> models.ForeignKey or make it optional.
>
> My model is like this:
> class Node(models.Model):
>     name = models.CharField(max_length=100)
>     parent = models.ForeignKey('self')
>
> runserver doesn't complain, but whenever I try to enter data to the
> model with Django's admin interface I get a "This field is required."
> message, and it want's me to select a entry, but there are none.
>
> --
> Magnus

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 21, 2010 15:41:23

Karen T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

optional ForeignKey


On Mon, Jun 21, 2010 at 10:37 AM, Magnus Valle <wisca...@gmail.com> wrote:

> Hi,
> I'm trying to make model that has itself as a foreign key, in an
> attempt to store a reverse tree in Django.
> However, I can't find a way to set a default value to
> models.ForeignKey or make it optional.
>
> My model is like this:
> class Node(models.Model):
> name = models.CharField(max_length=100)
> parent = models.ForeignKey('self')
>
> runserver doesn't complain, but whenever I try to enter data to the
> model with Django's admin interface I get a "This field is required."
> message, and it want's me to select a entry, but there are none.
>


You make a ForeignKey field optional by specifying blank=True, null=True:http://docs.djangoproject.com/en/1.2/ref/models/fields/#nullKaren
--http://tracey.org/kmt/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 21, 2010 16:10:33

Magnus V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

optional ForeignKey


Ah, OK, Thanks!
I didn't know that blank and null are available for all field-types, I
should have known.

Thanks!!

On Mon, Jun 21, 2010 at 15:41, Karen Tracey <kmtra...@gmail.com> wrote:
> On Mon, Jun 21, 2010 at 10:37 AM, Magnus Valle <wisca...@gmail.com> wrote:
>>
>> Hi,
>> I'm trying to make model that has itself as a foreign key, in an
>> attempt to store a reverse tree in Django.
>> However, I can't find a way to set a default value to
>> models.ForeignKey or make it optional.
>>
>> My model is like this:
>> class Node(models.Model):
>>    name = models.CharField(max_length=100)
>>    parent = models.ForeignKey('self')
>>
>> runserver doesn't complain, but whenever I try to enter data to the
>> model with Django's admin interface I get a "This field is required."
>> message, and it want's me to select a entry, but there are none.
>
>
> You make a ForeignKey field optional by specifying blank=True, null=True:
>
>http://docs.djangoproject.com/en/1.2/ref/models/fields/#null>
> Karen
> --
>http://tracey.org/kmt/>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>



--
Magnus

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 19th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support