Login

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

Author:
fish2000
Posted:
April 19, 2010
Language:
Python
Version:
1.1
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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 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.