Login

ISBN model field: displays 10- and 13-digit variants and external links

Author:
fish2000
Posted:
April 19, 2010
Language:
Python
Version:
1.1
Tags:
django model python widget modelfield isbn books
Score:
0 (after 0 ratings)

Requires PyISBN. Use like so:

class Book(models.Model):
    title = models.TextField()
    isbn = ISBNField()

... the link in the widget can be changed to amazon, borders, you name it. If the DB version is a 13-digit ISBN, the display box contains the 10-digit, labeled; and vice-versa.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
###################################### modelfields.py ######################################

class FSISBNField(models.CharField):
	def __init__(self, *args, **kwargs):
		kwargs['max_length'] = 13
		super(FSISBNField, self).__init__(*args, **kwargs)
	def formfield(self, **kwargs):
		kwargs['widget'] = FSAltISBNWidget()
		return super(FSISBNField, self).formfield(**kwargs)

###################################### widgets.py #########################################

class AltISBNWidget(forms.TextInput):
	def __init__(self, language=None, attrs={}):
		super(AltISBNWidget, self).__init__(attrs=attrs)
	def render(self, name, value, attrs={}):
		if (not value) or len(str(value)) < 2:
			return super(FSAltISBNWidget, self).render(name, value, attrs)
		return super(FSAltISBNWidget, self).render(name, value, attrs) + mark_safe("""
			<br />
			<div class="alt">%s-digit: <span class="altisbn">%s <span class="doodad">
				<a href="http://www.librarything.com/isbn/%s">more &#9901;</a>
			</span></span></div>
		""" % (
			(len(str(value)) == 10) and "13" or "10",
			pyisbn.convert(value),
			value
		))

More like this

  1. SelectTimeWidget by bradmontgomery 6 years, 3 months ago
  2. Showell markup--DRY up your templates by showell 5 years, 3 months ago
  3. Username form field by sma 6 years, 3 months ago
  4. Admin Image Widget by baumer1122 6 years, 7 months ago
  5. Pattern to integer list function by marinho 7 years, 3 months ago

Comments

miernik (on January 22, 2011):

Why is it called FSISBNField() but the instruction on the right is to write isbn = ISBNField()?

What is this "FS" about?

I think all FS should be removed from the source.

#

Please login first before commenting.