Model field that links to a page ID, or external URL

48 views
Skip to first unread message

Diederik van der Boor

unread,
Feb 13, 2012, 11:09:17 AM2/13/12
to django...@googlegroups.com
Hi,

In CMS interfaces I generally encounter a problem with URL fields.
In most situations, the following options need to be supported:
- An URL to an external page (Django's URLField can handle that)
- An URL to an internal page (e.g. Django CMS)
- An URL to an other model (e.g. article)

How can I reasonably store this information in a model field?
I would love to store these values as foreign key, as it would allow more querying options.

I've written a solution for this before using a forms.MultiValueField / MultiWidget input,
which did involve the ugly hack of saving the contents as string, using the format:
- http://...external-page/
- pageid://## -- the internal page
- articleid://## -- link to an article.
This works, and did fit in the project deadline.
However, I would like to improve upon this.

Does anyone have a suggestion on how to store the various information properly?

Greetings,
Diederik

Jonathan Paugh

unread,
Feb 13, 2012, 12:50:03 PM2/13/12
to django...@googlegroups.com
Each model supports a get_absolute_url(self) method, which allows you to
retrieve each object's url in whatever way you want, including pulling
values from self (i.e. the model object.). Returned urls are in serveral
formats; my prefered is '/full/path/to/article'.

Diederik van der Boor

unread,
Feb 14, 2012, 10:14:03 AM2/14/12
to django...@googlegroups.com
Op 13 feb. 2012, om 18:50 heeft Jonathan Paugh het volgende geschreven:

Each model supports a get_absolute_url(self) method, which allows you to
retrieve each object's url in whatever way you want, including pulling
values from self (i.e. the model object.). Returned urls are in serveral
formats; my prefered is '/full/path/to/article'.

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


I'm reluctant to use such method, when the absolute URL changes, the links break.
With a "pageid://##" solution I dynamically get get_absolute_url() when it's read.

Also, this solution gives me another problem: the URL needs to be reversed to find the ID
which was used in the select box / raw_id_field to choose the model.

if the solution still involves storing the value in a string field,
I still prefer my solution of storing the ID over a copied URL.

Diederik

Andy Baker

unread,
Oct 10, 2012, 1:15:00 PM10/10/12
to django...@googlegroups.com
(long dead thread resuscitation attempt)

I'm not sure I understand your objections. Why would absolute url change? And if it does you still have to worry about external links breaking.

Surely the answer is to ensure you don't change the value of get_absolute_url. Don't base it on anything that's likely to change.
Reply all
Reply to author
Forward
0 new messages