class Trip(models.Model):
trip_number = models.CharField(
_("Trip Number"), max_length=128, db_index=True, unique=True,
blank=True)
what i am looking is to make this trip_number as hyperlink.
To answer your question, with the new migration introduced in Django 1.7, in order to add a new field to a model you can simply add that field to your model and initialize migrations with ./manage.py makemigrations and then run ./manage.py migrate and the new field will be added to your DB. Save this answer.
In this article, we show how to create a URLField in Django. A URLField is a field (of a database table or a form) that stores only URLs. This could be useful for all types of reasons. One glaring example is to let a user enter his or her website.
CharField is a string field, for small- to large-sized strings. It is like a string field in C/C+++. CharField is generally used for storing small strings like first name, last name, etc. To store larger text TextField is used. The default form widget for this field is TextInput.
TimeStampedModel - An Abstract Base Class model that provides self-managed created and modified fields.
I think you can use URLField to store hyperlinks, since this field type use URLValidator. URLField is a subclass of CharField, so you can use max_length.
class Trip(models.Model):
trip_number = models.URLField(
_("Trip Number"),
max_length=128,
db_index=True,
unique=True,
blank=True
)
in the django admin you can use a function to display the value as a link. Here an example:
@admin.register(models.Trip)
class TripAdmin(admin.ModelAdmin):
list_display = ('id', 'trip_number', 'trip_link')
def trip_link(self, obj):
if obj.trip_number:
return "<a href='%s'>Link</a>" % obj.trip_number
else:
return ''
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With