On 17 oct, 17:45, Oleg Oltar <oltarase...@gmail.com> wrote:
> Hi
>
> I am trying to create a model for Article site. I want to link each article
> with 3-5 related articles, so what I am thinking of is creating code this
> way:
>
>     class Article (models.Model):
>         # Tiny url
>         url = models.CharField(max_length = 30, unique=True)
>         is_published = models.BooleanField()
>         author = models.CharField(max_length = 150)
>         title = models.CharField(max_length = 200)
>         short_description = models.TextField(max_length = 600)
>         body = tinymce_models.HTMLField()
>         related1 = models.ForeignKey(Article)
>         related2 = models.ForeignKey(Article)
>         related3 = models.ForeignKey(Article)
>
> But not sure if it's possible to make a foreign key relation to the same
> model.

It is of course possible, and it's documented here:

http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey

But:

>Also if for example, I will decide to bind 6, 7 articles together,
> how that will work, do I have to write related4, 5, 6....in the model? I
> want to have more common solution, so if I binding more articles, I don't
> need to redefine code again and again

A very sensible concern !-)

And the solution is obviously to use a many to many relationship - as
documented here:

http://docs.djangoproject.com/en/dev/ref/models/fields/#manytomanyfield

You'll probably want to make this relationship symetrical (as
explained in the FineManual).

HTH
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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
-~----------~----~----~----~------~----~------~--~---

Reply via email to